Graph
Ooi Wei Tsang School of Computing, NUS
1
Graph Ooi Wei Tsang School of Computing, NUS 1 A graph consists - - PowerPoint PPT Presentation
Graph Ooi Wei Tsang School of Computing, NUS 1 A graph consists of edges and vertices. 2 v u A vertex u is a neighbor of v, if there is an edge from v to u. We say u is adjacent to v. The number of neighbors of a vertex is called degree.
Ooi Wei Tsang School of Computing, NUS
1
A graph consists of edges and vertices.
2
A vertex u is a neighbor of v, if there is an edge from v to u. We say u is adjacent to v. The number
v u
3
A weighted graph has a value associated with its edges.
4 3
2
4
Direction of edges does not matter in a undirected graph.
5
In a complete graph, every vertex is connected to every other vertices.
6
A path consists of a sequence of vertices adjacent to each other.
7
A cycle is a path that starts and ends with the same vertex.
8
A graph is acyclic if it contains no cycle. It is cyclic otherwise.
9
A undirected graph is connected if there is a path between any two vertices.
10
A undirected graph is bipartite if we can partition the vertices into two sets and there are no edges between two vertices of the same set.
11
A unconnected graph consists of two connected components.
12
A connected, undirected, acyclic graph is called a tree.
13
A weighted graph G = (V, E, w),
14
V = { a, b, c } E = { (a,b), (c,b), (a,c) } w = { ((a,b), 4), ((c, b), 1), ((a,c),-3) } a b c 4
1
15
adj(v) : set of vertices adjacent to vertex v adj(a) = {b, c} adj(b) = { } adj(c) = {b} a b c 4
1
16
complete graph with N vertices?
17
|adj(u)| = ?
18
19
Representing a social network. (u,v) in E if u knows v.
20
Jeffrey Heer’s Social Network from Friendster (47471 people, 432430 edges)
21
Social network of 9/11 terrorists
22
Representing places and routes. (u,v) exists if there is a direct route from u to v. Weight w(u,v) is the distance or cost. We are
two places.
4 3 3 1 2 2
23
24
25
26
Possible moves in Rush Hour. Blue represents solutions. Green represents the shortest paths to solving the puzzle.
(from www.aisee.com)
27
28
1 2 4
1 Adjacency Matrix: Use a 2D array. Store w(u,v) in a[u] [v] if edge (u,v) exists. Store an invalid value otherwise.
4
1
1 2 1 2
29
1 2 4
1 Adjacency List: Use an array of link list. a[u] stores adj(u) and the associated weight.
1 2 1,4 2,-3 1,1
30
(a) adjacency matrix ? (b) adjacency list ?
31
neighbors of a vertex v for (a) adjacency matrix ? (b) adjacency list ?
32
(a) adjacency matrix ? (b) adjacency list ?
33
1 2 4
1 Adjacency List in Matrix: Use a 2D array. Each row is an array-representation of the adjacency list.
1,4 2,-3 1,1
1 2
34
Avoid using pointers in competitive programming. Most of the time, graph are static (no insert/ delete after initialization). Maximum size is often given.
35
typedef struct neighbor { int id; int weight; } neighbor; // N is max num of vertices; neighbor graph[N][N]; int num_of_vertices;
36
1 2 4
1 Edge List: Use a linked list of edges.
1,4 2,-3 1,1
37
1 2 4
1 Edge List: Use a array of edges.
1,4 2,-3 1,1 2 1
Edges Degree
38
typedef struct edge { int from; int to; int weight; } edge; edge graph[MAX_NUM_OF_EDGES]; int num_of_edges;
39
Pick the simplest implementation that meets the requirements.
40
How to systematically visit the whole graph?
41
42
vertices in increasing distance from the source
the shortest path from u to v consists of k edges.
43
F D E A C B
Example: F is 3-hop away from A. E is 2-hop away from A.
44
F D E A C B
Let A be the source. We first visit the source. I colored visited vertices yellow.
45
F D E A C B
Next, visit the vertices that are one-hop away.
46
F D E A C B
Next, visit the vertices that are two hops away. (i.e, all unvisited vertices that are neighbors of one-hop neighbor of A.
47
F D E A C B
Edges that lead to undiscovered node during traversal are colored brown.
48
3 2 2 1 2
These edges form the breadth-first tree. Level of vertices in the tree is the hop distance from source.
49
vertices we have discovered.
distance, we need to visit the nodes the order we discover them (FIFO).
50
F D E A C B
51
first tree by marking the edges in the tree as
algorithm?
52
F D E A C B
53
distance from the source. How should we change the algorithm?
54
F D E A C B
55
previous algorithm?
56
F D E A C B
57
F D E A C B
If we pick F as the source, then we can’t visit A, B, and C, and need to visit them through another source.
58
Mark all vertices as unvisited for each vertex v if v is not visited use v as source and run BFS
59
60
3 2 2 1 2
On an unweighted graph, the breadth-first tree tells us the shortest path from source to all the other vertices.
61
2 2 2 1 1
The algorithm works for undirected graph too.
62
? We can check if two vertices are connected using BFS.
63
64
repeatedly visit a neighbor of the current vertex until we hit a dead-end (no unvisited neighbors), then backtrack.
reachable from v.
65
F D E A C B
Let A be the source.
66
F D E A C B
Visit a neighbor of A (say, C).
67
F D E A C B
Visit a neighbor of C (say, E).
68
F D E A C B
Visit a neighbor of E (say, D).
69
F D E A C B
D has no neighbor. Back to E. E has no unvisited neighbor. Back to C.
70
F D E A C B
Visit B.
71
F D E A C B
Visit F.
72
F D E A C B
F has no unvisited neighbor. Back to B. B has no unvisited neighbor. Back to C.
73
F D E A C B
C has no unvisited neighbor. Back to A. A, the source, has no unvisited neighbor. Done!
74
vertices we have discovered.
last vertex we visited. (LIFO).
75
F D E A C B
76
Mark all vertices as unvisited for each vertex v if v is not visited use v as source and run DFS
77
F D E A C B
(a) before it is inserted into the stack ? (b) while it is inside the stack ? (c) after it is pop from the stack ?
D has no neighbor. Back to E. E has no unvisited neighbor. Back to C.
78
F D E A C B
A vertex can be in three states: unvisited, visiting, visited.
D has no neighbor. Back to E. E has no unvisited neighbor. Back to C.
79
F D E A C B
80
F D E A C B
81
vertices in the stack to the vertex at the top
vertex marked “visiting” to the current vertex.)
82
83
? We can check if two vertices are connected using DFS.
84
? We can check if a graph is acyclic/cyclic using DFS.
85
? There is a cycle iff we found an edge from current vertex to a visiting vertex (called backward edge)
86
87
Goal: Given a directed acyclic graph, order the vertices such that if there is a path from u to v, then u appears before v in the output.
88
F D E A C B
BACFED? BCAFED? BFACED? Goal: Given a directed acyclic graph, order the vertices such that if there is a path from u to v, then u appears before v in the output.
89
F D E A C B
Idea: The first vertex marked “visited” can appear last in the topological order.
90
F D E A C B
Now, we remove that vertex from consideration, and repeat -- the next vertex marked as visited can appear last in the topological sort order.
91
To output in topological sort order, pop from stack and print after completing DFS.
92
93
vertex v in G, find the shortest (or least cost) path from v to all other vertices.
94
F D E A C B
Shortest Path from A to D = A-C-E-D (Cost = 8) 5 5 1 3 1 2 3 1 4
95
distance.
96
10 6
Let d[v] be the current known shortest distance from u to v. d[v] = 6, d[w] = 10 2 u v w
97
8 6
We just found a shorter path from u to w. Update d[w] = d[v] + cost(v,w). We call this step relax(v,w). 2 u v w
98
proc relax (v,w): Let d = d[v] + cost(v,w) if d[w] > d d[w] = d
99
8 6
If d[w] is the smallest among the “remaining” vertices, then d[w] is the smallest possible (can’t be relaxed further) u
6 12 11
w
100
∞ ∞ ∞ ∞ ∞
At the beginning, we know d[A]. But the rest is unknown and is set to infinity. 5 5 1 3 1 2 3 1 4
101
∞ ∞ ∞ 5 ∞
Relax all neighbors of A. 5 5 1 3 1 2 3 1 4
102
∞ ∞ ∞ 5 ∞
Pick a white vertex with smallest d[ ]. Color it yellow. 5 5 1 3 1 2 3 1 4
103
∞ 10 6 5 8
Relax all neighbors of this vertex. 5 5 1 3 1 2 3 1 4
104
∞ 10 6 5 8
Repeat: pick a white vertex with smallest d[ ]. 5 5 1 3 1 2 3 1 4
105
∞ 8 6 5 8
Relax its neighbors 5 5 1 3 1 2 3 1 4
106
∞ 8 6 5 8
5 5 1 3 1 2 3 1 4
107
11 8 6 5 8
5 5 1 3 1 2 3 1 4
108
11 8 6 5 8
5 5 1 3 1 2 3 1 4
109
11 8 6 5 8
5 5 1 3 1 2 3 1 4 Everyone is yellow. Done!
110
proc Dijkstra(s): for each vertex v in G d[w] = infinity color[w] = white d[s] = 0
111
while there exists a white vertex let u be a white vertex with smallest d color[u] = yellow for each neighbor v of u relax(u,v)
112
while there exists a white vertex min = infinity for each vertex v if color[v] is white and d[v] < min min = d[v] u = v color[u] = yellow for each neighbor v of u
113
while there exists a white vertex u = q.getMin() color[u] = yellow for each neighbor v of u relax(u,v)
114
proc relax (v,w): Let d = d[v] + cost(v,w) if d[w] > d d[w] = d q.decreaseCost(w, d)
115
116
a weighted graph with positive weights.
117