Interactive Powerpoint_How to Master effective communication
Graph Representation, DFS and BFS Presentation.pptx
1.
2. Now I have N friends, but I have the phone
numbers of some of them only
I want to deliver a message to Sarah by
repeated calling
My memory is so good that I remember who
can be contacted by my N friends
Give me a calling plan for me and my friends
3. This is actually a graph!
Gary
Joyce
Ryan
John
Ada
Sarah
Winnie
4. A set of vertices (or nodes) linked by edges
Mathematically, we often write G = (V,E)
V: set of vertices, so |V| = number of vertices
E: set of edges, so |E| = number of edges
1
3
4
2 5
5. A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices inV
An edge e = (u,v) is a pair of vertices
Example:
a b
c
d e
V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e),
(d,e)}
6. To present the relationships between
different objects/elements in a mathematical
way
Examples:
Friendship
Local Area Network (LAN)
Map of a country
What could the vertices and edges represent
in the above examples?
8. • Directed (digraphs) /undirected graphs
• You may treat each undirected edge as two
directed edges in opposite directions
– Undirected: edge (u, v) = (v, u), (No self loops.)
– Directed: (u, v) is edge from u to v, denoted as u
v. (Self loops are allowed).
10. Vertices “u” and “v” are said to be adjacent if
there is an edge e = {u, v} joining them.
the edge “e” is said to be incident on each of
its vertices u and v.
11. V={0,1,2,3,4}
E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)}
0
1
4
2
3
When (x,y) is an edge,
we say that x is adjacent to y, and y is
adjacent from x.
0 is adjacent to 1.
1 is not adjacent to 0.
2 is adjacent from 1.
12. e3
v2
v1
v3
e2
e1
e4
1. Vertex set = {v1, v2, v3, v4, v5, v6}
2. Edge set = {e1, e2, e3, e4, e5, e6, e7}
3. e1, e2, and e3 are incident on v1
4. v2 and v3 are adjacent to v1
5. e6 and e7 are loops
6. e2 and e3 are parallel
7. v4 is an isolated vertex
v4
v6
v5
e5
e7
e6
13. Two standard ways.
Adjacency Lists.
Adjacency Matrix.
a
d
c
b a
b
c
d
b
a
d
d c
c
a b
a c
a
d
c
b
1 2
3 4
1 2 3 4
1 0 1 1 1
2 1 0 1 0
3 1 1 0 1
4 1 0 1 0
16. CS 103
A graph of n nodes is represented by a one-
dimensional array L of linked lists, where
L[i] is the linked list containing all the nodes
adjacent from node i.
The nodes in the list L[i] are in no particular order
a
d
c
b a
b
c
d
b
a
d
d c
c
a b
a c
18. N vertices, N linked lists
Each list stores its adjacent vertices
1
3
4
2 5
5
-2
7
4
0
1 5|-2 3|5
2 1|0 3|7
3
4 2|4
5
19. A graph of n nodes is represented by a one-
dimensional array L of linked lists, where
L[i] is the linked list containing all the nodes
adjacent from node i.
The nodes in the list L[i] are in no particular order
a
d
c
b a
b
c
d
b
a
d
d c
c
a b
a c
20. Pros:
Space-efficient.
Saves on space (memory): the representation takes as
many memory words as there are nodes and edge.
Total storage:(V+E)
Cons:
It can take up to O(n) time to determine if a pair of
nodes (i,j) is an edge: one would have to search the
linked list L[i], which takes time proportional to the
length of L[i].
(V) in the worst case.
21. Represented by a two dimensional array.
|V| |V| matrix A.
A is then given by:
otherwise
0
)
,
(
if
1
]
,
[
E
j
i
a
j
i
A ij
a
d
c
b
1 2
3 4
1 2 3 4
1 0 1 1 1
2 1 0 1 0
3 1 1 0 1
4 1 0 1 0
22. Pros:
Simple to implement
Easy and fast to tell if a pair (i,j) is an edge: simply
check if A[i][j] is 1 or 0
Preferred when graph is dense
▪ |E| is close to |V|2
Cons:
No matter how few edges the graph has, the
matrix takes O(n2) in memory-Space: (V2).
23. The amount of memory required is
O(V2)
For undirected graph to cut down needed memory
only entries on and above diagonal are saved
Adjacency Matrix
24. For undirected graph
The degree of a vertex is the number of edges incident
to that vertex.
For directed graph,
the in-degree of a vertex v is the number of
edges coming to have v as the head
the out-degree of a vertex v is the number of
edges that have v as the tail
A node with indegree 0 is a root.
26. simple path: no repeated vertices
cycle: simple path, except that the last vertex is the same as the
first vertex
a b
c
d e
b e c
27. For Undirected Graph
cycle: simple path, except that the last vertex is the same as the
first vertex
For Digraph
Loop: An edge terminates at the same vertex from which it has
started a b
c
d e
adca
28. An undirected graph is said to be connected if there is a
path between every pair of nodes. Otherwise, the graph
is disconnected.
Informally, an undirected graph is connected if it hangs in
one piece
Disconnected Connected
30. A graph is connected if there is a path from every vertex to
every other vertex.
A directed graph with this property is strongly connected.
If a directed graph is not strongly connected, but underlying
undirected graph is connected then the directed graph is weakly
connected.
31. A directed graph is weakly connected if there is an
undirected path between any pair of vertices, and strongly
connected if there is a directed path between every pair of
vertices.
A digraph that is not weakly connected is said to be
disconnected.
Disconnected
Weakly Connected
Strongly Connected
32. Let n = #vertices, and m = #edges
A complete graph: one in which all pairs of vertices are
adjacent
How many total edges in a complete graph?
m = n(n -1)/2.
Therefore, if a graph is not complete, m < n(n -1)/2
n 5
m (5
n 5
m (5
33. Let n = #vertices, and m = #edges
A complete Digraph: has directed edge from every vertex
to every other vertex .
How many total edges in a complete graph?
m = n(n -1).
Therefore, if a graph is not complete, m < n(n -1)
a
c
b
d
m=4(4-1)=12
34. Tree: either one of the followings is the
definition
A connected graph with |V|-1 edges
A connected graph without cycles
A graph with exactly one path between every pair
of vertices
35. Tree edges could be directed or undirected
For trees with directed edges, a root usually
exists
36. Tree - connected graph without cycles
Forest - All connected components are trees. Forest
is a collection of trees.
How many trees are there in the following
forest?
37. tree - connected graph without cycles
forest - collection of trees
tree
forest
tree
tree
tree
38. Searching a graph means systematically
following the edges of the graph so as to visit
the vertices.
Standard graph-searching algorithms.
Breadth-first Search (BFS).
Depth-first Search (DFS).
39.
40. Go to all nearest nodes first
The data structure “queue” is used to store
the visited nodes
Expand from visited (but not dead) nodes.
42. Expands between discovered and undiscovered vertices
uniformly across the breadth.
A vertex is “discovered” the first time it is encountered during
the search.
A vertex is “finished” if all vertices adjacent to it have been
discovered.
Color the vertices to keep track of progress.
– Undiscovered.
Gray – Discovered but not finished.
Black – Finished.
▪ Colors are required only to reason about the algorithm.Can be
implemented without colors.
43. Breadth-first search constructs a breadth-first tree, initially
containing only its root, which is the source vertex s.
Whenever the search discovers a white vertex in the course
of scanning the adjacency list of an already discovered
vertex u, the vertex v and the edge (u,v) are added to the
tree.
We say that u is the predecessor or parent of v in the
breadth-first tree.
Ancestor and descendant relationships in the breadth-first
tree are defined relative to the root.
If u is on the simple path in the tree from the root s to
vertex v , then u is an ancestor of v and v is a descendant of
u.
44. It is assumed that input graph G (V, E) is represented
using adjacency list.
Additional attributes are attached to each vertex in the
graph:
color[u] – stores color of each vertex
π[u] – stores predecessor of u
d[u] – stores distance from source s to vertex u
45. BFS(G, s)
1 for each vertex u Є G: V [G] – {s}
2 do color [u] ← WHITE
3 d [u] ← ∞
4 π[u] ← NIL
5 color[s] ← GRAY
6 d [s] ← 0
7 π[s] ← NIL
8 Q ← Ø /* Q always contains the set of GRAY vertices */
9 ENQUEUE (Q, s)
10 while Q ≠ Ø
11 do u ← DEQUEUE (Q)
12 for each v Є G. Adj [u]
13 do if color [v] = WHITE /* For undiscovered vertex. */
14 then color [v] ← GRAY
15 d [v] ← d [u] + 1
16 π[v] ← u
17 ENQUEUE(Q, v)
18 color [u] ← BLACK
46.
r s t u
v w x y
Q
Except root node, s
For each vertex u Є V(G)
color [u] ← WHITE
d[u] ← ∞
π [s] ← NIL
47.
0
r s t u
v w x y
s
Q
Considering s as root node
color[s] ← GRAY
d[s] ← 0
π [s] ← NIL
ENQUEUE (Q, s)
48. 1
0
1
r s t u
v w x y
DEQUEUE s from Q
Adj[s] = w, r
color [w] = WHITE
color [w] ← GRAY
d[w]←d[s]+1 =0+1 = 1
π[w] ← s
ENQUEUE (Q, w)
color [r] = WHITE
color [r] ← GRAY
d [r] ← d [s] + 1 = 0 + 1 = 1
π[r] ← s
ENQUEUE (Q, r)
color [s] ← BLACK
w
Q r
49. 1
0
1
2
2
r s t u
v w x y
DEQUEUE w from Q
Adj[w] = s, t, x
color [s] ≠ WHITE
color [t] = WHITE
color [t] ← GRAY
d [t] ← d [w] + 1 = 1 + 1 = 2
π[t] ← w
ENQUEUE (Q, t)
color [x] = WHITE
color [x] ← GRAY
d [x] ← d [w] + 1 = 1 + 1 = 2
π[x] ← w
ENQUEUE (Q, x)
color [w] ← BLACK
r
Q t x
50. 1
2
0
1
2
2
r s t u
v w x y
DEQUEUE r from Q
Adj[r] = s, v
color [s] ≠ WHITE
color [v] = WHITE
color [v] ← GRAY
d [v] ←d [r] +1 =1+1 = 2
π[v] ← r
ENQUEUE (Q, v)
color [r] ← BLACK
Q t x v
51. DEQUEUE t from Q
Adj[t] = u, w, x
color [u] = WHITE
color [u] ← GRAY
d [u] ← d [t]+1 = 2+1 =3
π[u] ← t
ENQUEUE (Q, u)
color [w] ≠ WHITE
color [x] ≠ WHITE
color [t] ← BLACK
1
2
0
1
2
2
3
r s t u
v w x y
Q x v u
52. DEQUEUE x from Q
Adj[x] = t, u, w, y
color [t] ≠ WHITE
color [u] ≠ WHITE
color [w] ≠ WHITE
color [y] = WHITE
color [y] ← GRAY
d [y] ← d [x] + 1 =2+1 =3
π[y] ← x
ENQUEUE (Q, y)
color [x] ← BLACK
1
2
0
1
2
2
3
3
r s t u
v w x y
Q v u y
53. DEQUEUE v from Q
Adj[v] = r
color [r] ≠ WHITE
color [v] ← BLACK
1
2
0
1
2
2
3
3
r s t u
v w x y
Q u y
54. DEQUEUE u from Q
Adj[u] = t, x, y
color [t] ≠ WHITE
color [x] ≠ WHITE
color [y] ≠ WHITE
color [u] ← BLACK
1
2
0
1
2
2
3
3
r s t u
v w x y
Q y
55. DEQUEUE y from Q
Adj[y] = u, x
color [u] ≠ WHITE
color [x] ≠ WHITE
color [y] ← BLACK
1
2
0
1
2
2
3
3
r s t u
v w x y
Q
57. BFS(G, s)
1 for each vertex u Є G: V [G] – {s}
2 do color [u] ← WHITE
3 d [u] ← ∞
4 π[u] ← NIL
5 color[s] ← GRAY
6 d [s] ← 0
7 π[s] ← NIL
8 Q ← Ø /* Q always contains the set of GRAY vertices */
9 ENQUEUE (Q, s)
10 while Q ≠ Ø
11 do u ← DEQUEUE (Q)
12 for each v Є G. Adj [u]
13 do if color [v] = WHITE /* For undiscovered vertex. */
14 then color [v] ← GRAY
15 d [v] ← d [u] + 1
16 π[v] ← u
17 ENQUEUE(Q, v)
18 color [u] ← BLACK
Total Running Time = O(V + E)
O(V)
O(E) O(V+E)
58. Initialization takes O(V).
Traversal Loop
After initialization, each vertex is enqueued and dequeued at
most once, and each operation takes O(1). So, total time for
queuing is O(V).
The adjacency list of each vertex is scanned at most once. The
sum of lengths of all adjacency lists is (E).
Summing up over all vertices => total running time of
BFS is O(V+E), linear in the size of the adjacency list
representation of graph.
Total Running Time of BFS = O(V+E)
59. The shortest-path-distance δ (s, v) from s to v as
the minimum number of edges in any path from
vertex s to vertex v.
if there is no path from s to v, then δ (s, v) = ∞
A path of length δ (s, v) from s to v is said to be a
shortest path from s to v.
Breadth First search finds the distance to each
reachable vertex in the graph G (V, E) from a
given source vertex s є V.
The field d, for distance, of each vertex is used.
60. BFS(G, s)
1 for each vertex u Є G: V [G] – {s}
2 do color [u] ← WHITE
3 d [u] ← ∞
4 π[u] ← NIL
5 color[s] ← GRAY
6 d [s] ← 0
7 π[s] ← NIL
8 Q ← Ø /* Q always contains the set of GRAY vertices */
9 ENQUEUE (Q, s)
10 while Q ≠ Ø
11 do u ← DEQUEUE (Q)
12 for each v Є G. Adj [u]
13 do if color [v] =WHITE /* For undiscovered vertex. */
14 then color [v] ← GRAY
15 d [v] ← d [u] + 1
16 π[v] ← u
17 ENQUEUE(Q, v)
18 color [u] ← BLACK
61. BFS-Shortest-Paths (G, s)
1 for each vertex u є V [G] – {s}
2 d [u] ← ∞
3 d [s] ← 0
4 ENQUEUE (Q, s)
5 while Q ≠ φ
6 do u ← DEQUEUE(Q)
7 for each v in Adj[u]
8 do if d [v] = ∞
9 then d [v] ← d [u] +1
10 ENQUEUE (Q, v)
62. PRINT-PATH (G, s, v)
1 if v == s
2 print s
3 else if π[v] == NIL
4 print “no path from s to v exists
5 else PRINT-PATH (G, s, π[v])
6 print v
63. 1 0
1 2 3
2 3
2
r s t u
v w x y
BFSTree
PRINT-PATH (G, s, v)
1 if v == s
2 print s
3 else if π[v] == NIL
4 print “no path from s to v exists
5 else PRINT-PATH (G, s, π[v])
6 print v
64.
65. Let’s use recursion!:
Go as far as you can
If it is a blind end, go back and walk through
another edge
1
5
2
6
3
4
7
66. Let’s review the graph, and note the color
changes
White: Unvisited
Grey: Discovered
Black: Finished
(No more unvisited adjacent nodes)
1
5
2
6
3
4
7
67. Each vertex is initially white
It is grayed when it is discovered in the search, and
It is blackened when it is finished, that is, when its
adjacency list has been examined completely.
68. It guarantees that each vertex ends up in exactly one
depth-first tree, so that these trees are disjoint.
It timestamps each vertex
The first timestamp d[v] records when v is first
discovered (and grayed)
The second timestamp f [v] records when the search
finishes examining v's adjacency list (and blackens v).
For every vertex u
d[u] < f[u]
69. DFS(G)
1 for each vertex u є V [G]
2 do color [u] ← WHITE
3 π[u] ← NIL
4 time ← 0 //global variable
5 for each vertex u є V [G]
6 do if color [u] = WHITE
7 then DFS-Visit (u)
DFS-Visit(u)
1 color [u] ← GRAY
2 time ← time + 1
3 d [u] ← time
4 for each v є Adj [u]
5 if color [v]= WHITE
6 then π[v] ← u
7 DFS-Visit (v)
8 color [u] ← BLACK
9 f[u] ← time ← time + 1
Algorithm: Depth First Search
70. u v w
x y z
For each vertex u є V(G)
color [u] ← WHITE
π [u] ← NIL
time ← 0
71. 1/
u v w
x y z
Considering white vertex u
color [u] ← GRAY
d[u] ← time + 1 = 0 + 1 = 1
Adj[u] = v, x
If color [v] = WHITE
π[v] ← u
DFS-VISIT (v)
72. 1/ 2/
u v w
x y z
color [v] ← GRAY
d[v] ← time + 1 = 1 + 1 = 2
Adj[v] = y
If color [y] =WHITE
π[y] ← v
DFS-VISIT (y)
73. 1/ 2/
3/
u v w
x y z
color [y] ← GRAY
d[y] ← time + 1 = 2 + 1 = 3
Adj[y] = x
If color [x] = WHITE
π[x] ← y
DFS-VISIT (x)
74. 1/
4/
2/
3/
u v w
x y z
color [x] ← GRAY
d[x] ← time + 1 = 3 + 1 = 4
Adj[x] = v
color [v] ≠ WHITE
75. 1/
4/
2/
3/
u v w
x y z
The edge (x, v) is a back
edge that is a non tree edge
and is labeled as B
B
Back Edges: Those edges (u,v), connecting a vertex u to an ancestor v. Self loops in
directed graphs are also considered as back edges.
Forward Edges: Those non-tree edges (u,v), connecting a vertex u to an descendant v.
Cross Edges: All other edges, as long as one vertex is not an ancestor of the other.
76. 1/
4/5
2/
3/
u v w
x y z
B
The vertex x is finished.
color [x] ← BLACK
f[x] ← time + 1 = 4 + 1 = 5
77. 1/
4/5
2/
3/6
u v w
x y z
B
The vertex y is finished.
color [y] ← BLACK
f[y] ← time + 1 = 5 + 1 = 6
78. 1/
4/5
2/7
3/6
u v w
x y z
B
The vertex v is finished.
color [v] ← BLACK
f[v] ← time + 1 = 6 + 1 = 7
79. 1/
4/5
2/7
3/6
u v w
x y z
B
F
The edge (u, x) is a forward
edge that is a non tree edge
and is labeled as F
Back Edges: Those edges (u,v), connecting a vertex u to an ancestor v. Self loops in
directed graphs are also considered as back edges.
Forward Edges: Those non-tree edges (u,v), connecting a vertex u to an descendant v.
Cross Edges: All other edges, as long as one vertex is not an ancestor of the other.
80. 1/8
4/5
2/7
3/6
u v w
x y z
B
F
The vertex u is finished.
color [u] ← BLACK
f[u] ← time + 1 = 7 + 1 = 8
81. 1/8
4/5
2/7
3/6
9/
u v w
x y z
B
F
Considering white vertex w
color [w] ← GRAY
d[w] ← time +1= 8+1 = 9
Adj[w] = y, z
color [y] ≠ WHITE
If color [z]= WHITE
π[z] ← w
DFS-VISIT (z)
82. 1/8
4/5
2/7
3/6
9/
u v w
x y z
B
F C
The edge (w, y) is a cross
edge that is a non tree edge
and is labeled as C
Back Edges: Those edges (u,v), connecting a vertex u to an ancestor v. Self loops in
directed graphs are also considered as back edges.
Forward Edges: Those non-tree edges (u,v), connecting a vertex u to an descendant v.
Cross Edges: All other edges, as long as one vertex is not an ancestor of the other.
84. 1/8
4/5
2/7
3/6
9/
10/
u v w
x y z
B
F C B
The edge (z, z) is a back
edge that is a non tree edge
and is labeled as B
Back Edges: Those edges (u,v), connecting a vertex u to an ancestor v. Self loops in
directed graphs are also considered as back edges.
Forward Edges: Those non-tree edges (u,v), connecting a vertex u to an descendant v.
Cross Edges: All other edges, as long as one vertex is not an ancestor of the other.
87. DFS(G)
1 for each vertex u є V [G]
2 do color [u] ← WHITE
3 π[u] ← NIL
4 time ← 0
5 for each vertex u є V [G]
6 do if color [u] = WHITE
7 then DFS-Visit (u)
DFS-Visit(u)
1 color [u] ← GRAY
2 time ← time + 1
3 d [u] ← time
4 for each v є Adj [u]
5 if color [v]= WHITE
6 then π[v] ← u
7 DFS-Visit (v)
8 color [u] ← BLACK
9 f[u] ← time ← time + 1
Analysis: Depth First Search
Total Running Time = O(V + E)
O(V)
O(V)
O(E)
88. Initialization takes O(V).
DFS-Visit is called once for each white vertex vV
when it’s painted gray the first time. (E)
Total running time of DFS is (V+E).