SlideShare a Scribd company logo
1 of 88
 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
 This is actually a graph!
Gary
Joyce
Ryan
John
Ada
Sarah
Winnie
 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
 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)}
 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?
 electronic circuits
 networks (roads, flights, communications)
CS16
LAX
JFK
LAX
DFW
STL
HNL
FTL
• 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).
 Weighted/unweighted graphs
 You may treat unweighted edges to be
weighted edges of equal weights
5
-2
7
4
0
 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.
 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.
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
 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
graph Adjacency list Adjacency matrix
graph Adjacency list Adjacency matrix
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
 Use a 2D array
st 1 2 3 4 5
1 5 -2
2 0 7
3
4 4
5
1
3
4
2 5
5
-2
7
4
0
 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
 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
 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.
 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
 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).
 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
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.
0
1 2
3 4 5 6
G1
G2
3
2
3 3
1 1 1 1
directed graph
in-degree
out-degree
0
1
2
G3
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
0
1 2
3
3
3
3
Examples
 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
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
 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
connected not connected
•connected graph: any two vertices are connected by some path
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.
 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
 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 
 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
 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
 Tree edges could be directed or undirected
 For trees with directed edges, a root usually
exists
 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?
 tree - connected graph without cycles
 forest - collection of trees
tree
forest
tree
tree
tree
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).
 Go to all nearest nodes first
 The data structure “queue” is used to store
the visited nodes
 Expand from visited (but not dead) nodes.
1
5
2
6
3
4
7
Queue 1 6 2 5 7 3 4
The path has been found!
But let’s complete the
BFS…
 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.
 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.
 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
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

 




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


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)
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
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
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
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
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
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
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
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 
1 0
1 2 3
2 3
2
r s t u
v w x y
BFSTree
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)
 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)
 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.
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
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)
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
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
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
 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
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.
 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]
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
u v w
x y z
For each vertex u є V(G)
color [u] ← WHITE
π [u] ← NIL
time ← 0
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)
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)
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)
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
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.
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
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
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
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.
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
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)
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.
1/8
4/5
2/7
3/6
9/
10/
u v w
x y z
B
F C
color [z] ← GRAY
d[z] ← time + 1= 9+1 =10
Adj[z] = z
color [z] ≠ WHITE
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.
1/8
4/5
2/7
3/6
9/
10/11
u v w
x y z
B
F C B
The vertex z is finished.
color [z] ← BLACK
f[z]← time + 1 =10+1 = 11
1/8
4/5
2/7
3/6
9/12
10/11
u v w
x y z
B
F C B
The vertex w is finished.
color [w] ← BLACK
f[w] ← time +1 =11+1 =12
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)
 Initialization takes O(V).
 DFS-Visit is called once for each white vertex vV
when it’s painted gray the first time. (E)
 Total running time of DFS is (V+E).

More Related Content

Similar to Graph Representation, DFS and BFS Presentation.pptx

lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptTalhaFarooqui12
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabatinabati
 
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13sumitbardhan
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
Ass. (3)graph d.m
Ass. (3)graph d.mAss. (3)graph d.m
Ass. (3)graph d.mSyed Umair
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theoryArvindBorge
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringSaurabh Kaushik
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data StructureProf Ansari
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfChristianKapsales1
 

Similar to Graph Representation, DFS and BFS Presentation.pptx (20)

lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.ppt
 
Graphs
GraphsGraphs
Graphs
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
 
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
 
10.graph
10.graph10.graph
10.graph
 
Graphs
GraphsGraphs
Graphs
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Ass. (3)graph d.m
Ass. (3)graph d.mAss. (3)graph d.m
Ass. (3)graph d.m
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
graph.ppt
graph.pptgraph.ppt
graph.ppt
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theory
 
Graphs
GraphsGraphs
Graphs
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
 
Graphs
GraphsGraphs
Graphs
 
Graph theory presentation
Graph theory presentationGraph theory presentation
Graph theory presentation
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
 

Recently uploaded

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
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?
  • 7.  electronic circuits  networks (roads, flights, communications) CS16 LAX JFK LAX DFW STL HNL FTL
  • 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).
  • 9.  Weighted/unweighted graphs  You may treat unweighted edges to be weighted edges of equal weights 5 -2 7 4 0
  • 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
  • 14. graph Adjacency list Adjacency matrix
  • 15. graph Adjacency list Adjacency matrix
  • 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
  • 17.  Use a 2D array st 1 2 3 4 5 1 5 -2 2 0 7 3 4 4 5 1 3 4 2 5 5 -2 7 4 0
  • 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.
  • 25. 0 1 2 3 4 5 6 G1 G2 3 2 3 3 1 1 1 1 directed graph in-degree out-degree 0 1 2 G3 in:1, out: 1 in: 1, out: 2 in: 1, out: 0 0 1 2 3 3 3 3 Examples
  • 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
  • 29. connected not connected •connected graph: any two vertices are connected by some path
  • 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.
  • 41. 1 5 2 6 3 4 7 Queue 1 6 2 5 7 3 4 The path has been found! But let’s complete the BFS…
  • 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 
  • 56. 1 0 1 2 3 2 3 2 r s t u v w x y BFSTree
  • 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.
  • 83. 1/8 4/5 2/7 3/6 9/ 10/ u v w x y z B F C color [z] ← GRAY d[z] ← time + 1= 9+1 =10 Adj[z] = z color [z] ≠ WHITE
  • 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.
  • 85. 1/8 4/5 2/7 3/6 9/ 10/11 u v w x y z B F C B The vertex z is finished. color [z] ← BLACK f[z]← time + 1 =10+1 = 11
  • 86. 1/8 4/5 2/7 3/6 9/12 10/11 u v w x y z B F C B The vertex w is finished. color [w] ← BLACK f[w] ← time +1 =11+1 =12
  • 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 vV when it’s painted gray the first time. (E)  Total running time of DFS is (V+E).