0% found this document useful (0 votes)
6 views

Algo8 Graph

Uploaded by

zxcvbnm5117123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Algo8 Graph

Uploaded by

zxcvbnm5117123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Algorithms

Pei-Yu Lin
[email protected]
Data Structure

Graph Tree

2
Graph

Relationships among a set of objects


3
Find a path: across all the edges exactly once

1 2

3 4

4
Find a path: across all the edges exactly once

1 2 3

4 5 6

5
Find a path: across all the edges exactly once

1 2 3

4 5 6

6
Find a path: across all the edges exactly once

1 2 3

4 5 6

7
Find a path: across all the edges exactly once
and return to the starting node

1 2

3 4

8
Find a path: across all the edges exactly once
and return to the starting node

1 2

3 4

9
Find a path: across all the edges exactly once
and return to the starting node

1 2 3

4 5 6

10
Find a path: across all the edges exactly once

Euler Path (尤拉路徑)


• 找出node的degree為odd的總數量: n
− 若 n = 0,任選一個點作為起點
− 若 n = 2,任選其中一個奇點當作起點
− 若 n > 2:無解

Find a path: across all the edges exactly once


and return to the starting node

Euler Circuit (尤拉迴圈)


• All node的degree 均為even
11
Konigsberg bridges Problem
• Seven Bridges Problem (七橋問題)
• Invented by Leonhard Euler, 1736
• Walk across all the bridges exactly once and return to
the starting land area?

12
13
Graph
Relationships among a set of objects

neural networks
Graph Theory Network Analysis Computer Network

values of node voltages, branch


voltages and branch currents

15
Graph
• A graph G = (V, E) consists of two sets V and E.
−V: nodes/vertices
−E: edges
e = {u, v} ∈ E for some u, v ∈ V

• Undirected graph
• Directed graph
• Weighted graph
16
undirected directed weighted
symmetric relationships asymmetric relationships weight relationships

u u
neighbor neighbor
v v

{u, v} = {v, u} (u, v) ≠ (v, u)


17
• Path
− a sequence P of nodes v1, v2, … vi, vi+1, …, vk in an undirected graph
G = (V, E)
− Each consecutive pair vi, vi+1 is joined by an edge in E, (vi, vi+1) ∈E
− A path is simple if all nodes are distinct P: 1, 2, 3, 6, 5, 8, 9, 6

• Cycle
simple 4
C: 1, 2, 3, 6, 5, 1
− a path v1, v2, … , vk-1, vk in which v1 = vk , k>2
1 2 3
− The first k-1 nodes are all distinct

• An undirected graph is connected if, for every 5 6


pair of nodes u and v, there is a path from u to v. 7
8
• Minimum distance 9
− The distance between nodes u and v is the minimum 10
number of edges in a u to v path
18
Representing Graph
• A graph G = (V, E)
−|V| = n : the number of nodes/vertices
−|E| = m: the number of edges
n
• Dense / Sparse : n-1 ≤ � ≤ n2
2
• Representing G: O(?)
− Linear time: O(n+m)

Adjacency Matrix Adjacency List


19
Adjacency Matrix
• A graph G = (V, E), and |V| = n, |E| = m
• The adjacency matrix of G is an nn matrix A,
− A[u, v]=1, if (u,v) ∈ E
symmetric
− A[u, v]=0, otherwise 1 2 3 4 5 6 7
1 0 1 1
• Time 2 1 0 1 1
− Check if (u, v) ∈ E : O(1) 3 1 0 1 1 1

− Find out all neighbors of u: O(n) 4 1 0


5 1 0 1
• Space: O(n2) 6 1 1 1 0 1
7 1 1 0
• Dense/Sparse graph ? 0’s for the others
Adjacency List
• A graph G = (V, E), and |V| = n, |E| = m
• The adjacency list of G is an array adj[ ] of n lists.
− adj[u] = a linked list of {v, if (u, v) ∈ E} Array
1 2 5
• Time degree of u
2 1 3 6
− Check if (u, v) ∈ E : O(deg(u)) 3 2 4 6 7
− Find out all neighbors of u: O(deg(u)) 4 3
n+2m
• Space: O(n+m) 5 1 6
6 2 3 5 7
• Dense/Sparse graph ? 7 3 6
Q

22
Q

A B C D E F
A
B
C
D
E
23
F
0 1 2 3 4 5 6 7

24
Graph & Tree
• An undirected graph is a tree if it is connected and
does not contain a cycle.

4 1

1 2 5 2
3
8 6 3
5 6
9 4 7
7
8 10
9
10
25
Hierarchy relationship
26
shortest-path ?

• Goal: 從 NKUST → Home


• Path:
− 從NKUST先走路到A點、坐33號公車到B點、坐38號公車到C點、
坐28號公車到Home
− 從NKUST先走路到A點、坐33號公車到D點、坐5號公車到C點、
坐28號公車到Home
− 從NKUST先走路到E點、坐44號公室到C點、坐28號公車到Home

27
shortest-path ?

E C

NKUST D Home

B
A

28
Shortest-Path Problem
• 找出兩點之間最短距離
− 寫西洋棋AI,用最少棋子移動步數贏得勝利
− 從朋友圈找到離你最近的正妹
− 撰寫拼字檢查,用最少訂正錯誤的字

29
Graph
Connectivity Traversal

Dora Peggy

Mia

Lucas William
Tim

Jack Alice
Bob

Elisa Diana 30
Rafa
Graph
Connectivity s to t connectivity problem
Alice to Lucas ?

Alice to Tim ?
Dora Peggy

Mia

Lucas William
Tim

Jack Alice
Bob

Elisa Diana 31
Rafa
Graph
• A connected component
Connectivity containing s is the set of nodes
that are reachable form s.

Dora Peggy

Mia

Lucas William
Tim

Jack Alice
Bob

Elisa Diana 32
Rafa
Graph
BFS
Connectivity Traversal DFS

Dora Peggy

Mia

Lucas William
Tim

Jack Alice
Bob

Elisa Diana 33
Rafa
Breadth-First Search (BFS)
• 廣度優先搜尋 (BFS),可用來解決兩類問題:

s to t connectivity problem
conne
ctivity
probl
s to t shortest-path problem
em

34
Breadth-First Search (BFS)
4th layer
3rd layer 2nd layer

• Start at s and flood the


1st layer
graph with an expanding Dora Peggy
wave that grows to visit
Mia
all nodes that it can reach.
Lucas William
Tim

Jack Alice
Bob

Elisa Rafa Diana

35
BFS Tree
4th layer
3rd layer 2nd layer
Layer (distance)
L0 Alice
1st layer
Tree edge Dora Peggy
L1 Mia William Diana Rafa Jack Mia

L2 Peggy Lucas Lucas William


Tim
Nontree edge
L3 Dora Jack Alice Nontree edge
Let x, y be nodes in BFS tree, Bob
L4 Bob and (x, y) be an edge of G.
Layer different: at mostElisa
1 Diana
Rafa
shortest-path 36
Breadth-First Search (BFS)

Dora Peggy
• A connectivity problem
Mia
containing s is the set of
nodes that are reachable form s. Lucas William
Tim

Jack Alice
Bob

Elisa Rafa Diana

Connected components 37
Connected component
• Find all nodes reachable from s:
Connected component(s)
1. initialize R={ s }
2. while (there is an edge (u, v) where u∈R and v∉R) do
3. R = R + {v}

s u v

R 38
Connected component

2
Connected components: 1 5
(1, 2, 6, 8, 5, 9, 10, 11, 7, 12)
(3, 4) 6 7
3 10
9
Time complexity? 8
Adjacency Matrix O(n2) 11
4 12
Adjacency List O(n+m)
39
shortest-path ?

E C

NKUST D Home

B
A

40
Q

A
41
Depth-First Search (DFS)
• 深度優先搜尋 (DFS)
• Start at s and try the first edge leading out, and
so on, until reach a dead end.

s to t connectivity problem
conne
ctivity
probl
em
42
Depth-First Search (DFS)

DFS(u)
1. Mark u as explored and add u to R Dora Peggy

2. for each edge (u, v) incident to u do Mia


3. if (v is not marked as explored) then
Lucas William
4. DFS(v) Tim

Jack Alice
Bob
s u v
Elisa Rafa Diana

R 43
DFS Tree
Layer (distance)
L0 Alice

L1 Diana
Dora Peggy
L2 Rafa William
Mia
Nontree edge
L3 Jack
Lucas William
Tim
L4 Lucas
Jack Alice
Nontree edge:
L5 Dora
Ancestor / Descendant Bob

L6 Bob Peggy Let x, y be nodes in DFS tree,


Elisa Rafa Diana
and (x, y) be a nontree edge.
L7 Mia x/y is an ancestor/descendant of the other 44
BFS & DFS
connectivity problem

Flat / Short Narrow / Deep

nontree edge
Layer different: at most 1 Ancestor / Descendant

shortest-path problem 45
Graph Representation A graph G = (V, E),
and |V| = n, |E| = m

Operations Adjacency Matrix Adjacency List


Storage Space O(n2) O(n+m)
Dense/sparse graph dense sparse
O(1)
Adding a vertex
O(n2), ∵(n2+n) O(n+m), ∵search the vertex: O(n),
Removing a vertex
traverse the edges: O(m)
Adding an edge O(1)
O(1)
Removing an edge O(m)

Querying an edge O(1) O(n)

Querying a degree O(n) O(degree(n))<O(n)

Traversing the graph O(n2) O(n+m)


46
Q

47
Q
A (C)

48
Q A

49
Q

A (1) c, a, d, e, b, i, f, h, k, g
(2) c, a, b, e, f, g, k, h, i, d
50
Color the nodes with two-coloring

two-coloring

51
Bipartite graphs
4
1 5
3 6
11
two-coloring problem 7 8
2
12 9 10
Given a graph G, is it bipartite?
• Color the nodes with two-coloring

• If a graph G is bipartite, then it cannot contain an odd cycle.

53
Bipartite graphs
• A graph G = (X, Y, E) whose nodes can be partitioned
into two sets
−X & Y: disjoint sets
− Every edge has one end in X and the other end in Y
− No two nodes within the same set are adjacent
X Y
players teams Social network
Medical field
workers jobs (lung cancer, throat cancer)
Coding theory
woman man Matching 54
Bipartite graphs Application of BFS

Input: G=(V, E) is connected A graph G = (V, E),


and |V| = n, |E| = m

Procedure:
1. Pick any node u∈V, and color it red
2. Color all the neighbors of u blue
3. Repeat coloring red/blue until the whole graph is colored

Test bipartiteness: every edge has ends of opposite colors

Time complexity: O( n+m )


55
Bipartite graphs Application of BFS

4 X Y
1 5 1 2
3 6
4 3
11
7 8 5 6
2 8 7

12 9 10
10 9

56
Given a graph G, is it bipartite?

57
Q

A (c), (d)

58

You might also like