SlideShare a Scribd company logo
1 of 39
Algorithm
Elementary Graph Algorithms
CSE215 2
Graphs - Background
Graphs = a set of nodes (vertices) with edges
(links) between them.
Notations:
• G = (V, E) - graph
• V = set of vertices V = n
• E = set of edges E = m
1 2
3 4
1 2
3 4
Directed
graph
Undirected
graph
1 2
3 4
Acyclic
graph
CSE215 3
Graph Representation
• Adjacency list representation of G = (V, E)
– An array of V lists, one for each vertex in V
– Each list Adj[u] contains all the vertices v such that
there is an edge between u and v
• Adj[u] contains the vertices adjacent to u (in arbitrary order)
– Can be used for both directed and undirected graphs
1 2
5 4
3
2 5 /
1 5 3 4 /
1
2
3
4
5
2 4
2 5 3 /
4 1 2
Undirected graph
CSE215 4
Graph Representation
• Adjacency matrix representation of G = (V, E)
– Assume vertices are numbered 1, 2, … V 
– The representation consists of a matrix A V x V :
– aij = 1 if (i, j)  E
0 otherwise
1 2
5 4
3
Undirected graph
1
2
3
4
5
1 2 3 4 5
0 1 1
0 0
1 1 1 1
0
1 1
0 0 0
1 1 1
0 0
1 1 1
0 0
For undirected
graphs matrix A
is symmetric:
aij = aji
A = AT
CSE215 5
Searching in a Graph
• Graph searching = systematically follow the
edges of the graph so as to visit the vertices of
the graph
• Two basic graph searching algorithms:
– Breadth-first search
– Depth-first search
– The difference between them is in the order in which
they explore the unvisited edges of the graph
• Graph algorithms are typically elaborations of
the basic graph-searching algorithms
CSE215 6
Breadth-First Search (BFS)
• Input:
– A graph G = (V, E) (directed or undirected)
– A source vertex s  V
• Goal:
– Explore the edges of G to “discover” every vertex
reachable from s, taking the ones closest to s first
• Output:
– d[v] = distance (smallest # of edges) from s to v, for
all v  V
– A “breadth-first tree” rooted at s that contains all
reachable vertices
CSE215 7
Breadth-First Search (cont.)
• Discover vertices in increasing order of distance
from the source s – search in breadth not depth
– Find all vertices at 1 edge from s, then all vertices at 2
edges from s, and so on
1 2
5 4
3
7
7
9
11
12
6
CSE215 8
Breadth-First Search (cont.)
• Keeping track of progress:
– Color each vertex in either white,
gray or black
– Initially, all vertices are white
– When being discovered a vertex
becomes gray
– After discovering all its adjacent
vertices the node becomes black
– Use FIFO queue Q to maintain the
set of gray vertices
1 2
5 4
3
1 2
5 4
3
source
1 2
5 4
3
CSE215 9
Breadth-First Tree
• BFS constructs a breadth-first tree
– Initially contains the root (source vertex s)
– When vertex v is discovered while scanning
the adjacency list of a vertex u  vertex v
and edge (u, v) are added to the tree
– u is the predecessor (parent) of v in the
breadth-first tree
– A vertex is discovered only once  it has at
most one parent
1 2
5 4
3
source
CSE215 10
BFS Additional Data Structures
• G = (V, E) represented using adjacency lists
• color[u] – the color of the vertex for all u  V
• [u] – predecessor of u
– If u = s (root) or node u has not yet been
discovered  [u] = NIL
• d[u] – the distance from the source s to
vertex u
• Use a FIFO queue Q to maintain the set of
gray vertices
1 2
5 4
3
d=1
=1
d=1
=1
d=2
=5
d=2
=2
source
CSE215 11
BFS(V, E, s)
1. for each u  V - {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  
9. Q ← ENQUEUE(Q, s)
Q: s
 0  
   
r s t u
v w x y
  
   
r s t u
v w x y
r s t u
v w x y
CSE215 12
BFS(V, E, s)
10. while Q  
11. do u ← DEQUEUE(Q)
12. for each v  Adj[u]
13. do if color[v] = WHITE
14. then color[v] = GRAY
15. d[v] ← d[u] + 1
16. [v] = u
17. ENQUEUE(Q, v)
18. color[u]  BLACK
 0  
 1  
r s t u
v w x y
Q: w
Q: s
 0  
   
r s t u
v w x y
1 0  
 1  
r s t u
v w x y
Q: w, r
CSE215 13
Example
1 0  
 1  
r s t u
v w x y
Q: s
 0  
   
r s t u
v w x y
Q: w, r
v w x y
1 0 2 
 1 2 
r s t u
Q: r, t, x
1 0 2 
2 1 2 
r s t u
v w x y
Q: t, x, v
1 0 2 3
2 1 2 
r s t u
v w x y
Q: x, v, u
1 0 2 3
2 1 2 3
r s t u
v w x y
Q: v, u, y
1 0 2 3
2 1 2 3
r s t u
v w x y
Q: u, y
1 0 2 3
2 1 2 3
r s t u
v w x y
Q: y
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: 
CSE215 14
Analysis of BFS
1. for each u  V - {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  
9. Q ← ENQUEUE(Q, s)
O(V)
(1)
CSE215 15
Analysis of BFS
10. while Q  
11. do u ← DEQUEUE(Q)
12. for each v  Adj[u]
13. do if color[v] = WHITE
14. then color[v] = GRAY
15. d[v] ← d[u] + 1
16. [v] = u
17. ENQUEUE(Q, v)
18. color[u]  BLACK
(1)
(1)
Scan Adj[u] for all vertices
in the graph
• Each vertex is scanned only
once, when the vertex is
dequeued
• Sum of lengths of all
adjacency lists = (E)
• Scanning operations:
O(E)
• Total running time for BFS = O(V + E)
CSE215 16
Shortest Paths Property
• BFS finds the shortest-path distance from the
source vertex s  V to each node in the graph
• Shortest-path distance = (s, u)
– Minimum number of edges in any path from s to u
r s t u
1 0 2 3
2 1 2 3
v w x y
source
CSE215 17
Applications of BFS
• Connected Components
– More than one source
• Two Coloring Graphs
– Bipartite matching
• Finding Shortest Path
CSE215 18
Depth-First Search
• Input:
– G = (V, E) (No source vertex given!)
• Goal:
– Explore the edges of G to “discover” every vertex in V
starting at the most current visited node
– Search may be repeated from multiple sources
• Output:
– 2 timestamps on each vertex:
• d[v] = discovery time
• f[v] = finishing time (done with examining v’s adjacency list)
– Depth-first forest
1 2
5 4
3
CSE215 19
Depth-First Search
• Search “deeper” in the graph whenever
possible
• Edges are explored out of the most recently
discovered vertex v that still has unexplored
edges
• After all edges of v have been explored, the search
“backtracks” from the parent of v
• The process continues until all vertices reachable from the
original source have been discovered
• If undiscovered vertices remain, choose one of them as a
new source and repeat the search from that vertex
• DFS creates a “depth-first forest”
1 2
5 4
3
CSE215 20
DFS Additional Data Structures
• Global variable: time-step
– Incremented when nodes are discovered/finished
• color[u] – similar to BFS
– White before discovery, gray while processing and
black when finished processing
• [u] – predecessor of u
• d[u], f[u] – discovery and finish times
GRAY
WHITE BLACK
0 2V
d[u] f[u]
1 ≤ d[u] < f [u] ≤ 2 |V|
CSE215 21
DFS(V, E)
1. for each u  V
2. do color[u] ← WHITE
3. [u] ← NIL
4. time ← 0
5. for each u  V
6. do if color[u] = WHITE
7. then DFS-VISIT(u)
• Every time DFS-VISIT(u) is called, u becomes the
root of a new tree in the depth-first forest
u v w
x y z
CSE215 22
DFS-VISIT(u)
1. color[u] ← GRAY
2. time ← time+1
3. d[u] ← time
4. for each v  Adj[u]
5. do if color[v] = WHITE
6. then [v] ← u
7. DFS-VISIT(v)
8. color[u] ← BLACK
9. time ← time + 1
10. f[u] ← time
1/
u v w
x y z
u v w
x y z
time = 1
1/ 2/
u v w
x y z
CSE215 23
Example
1/ 2/
u v w
x y z
1/
u v w
x y z
1/ 2/
3/
u v w
x y z
1/ 2/
4/ 3/
u v w
x y z
1/ 2/
4/ 3/
u v w
x y z
B
1/ 2/
4/5 3/
u v w
x y z
B
1/ 2/
4/5 3/6
u v w
x y z
B
1/ 2/7
4/5 3/6
u v w
x y z
B
1/ 2/7
4/5 3/6
u v w
x y z
B
F
CSE215 24
Example (cont.)
1/8 2/7
4/5 3/6
u v w
x y z
B
F
1/8 2/7 9/
4/5 3/6
u v w
x y z
B
F
1/8 2/7 9/
4/5 3/6
u v w
x y z
B
F
C
1/8 2/7 9/
4/5 3/6 10/
u v w
x y z
B
F
C
1/8 2/7 9/
4/5 3/6 10/
u v w
x y z
B
F
C
B
1/8 2/7 9/
4/5 3/6 10/11
u v w
x y z
B
F
C
B
1/8 2/7 9/12
4/5 3/6 10/11
u v w
x y z
B
F
C
B
The results of DFS may depend on:
• The order in which nodes are explored
in procedure DFS
• The order in which the neighbors of a
vertex are visited in DFS-VISIT
CSE215 25
Edge Classification
• Tree edge (reaches a WHITE
vertex):
– (u, v) is a tree edge if v was first
discovered by exploring edge (u, v)
• Back edge (reaches a GRAY
vertex):
– (u, v), connecting a vertex u to an
ancestor v in a depth first tree
– Self loops (in directed graphs) are
also back edges
1/
u v w
x y z
1/ 2/
4/ 3/
u v w
x y z
B
CSE215 26
Edge Classification
• Forward edge (reaches a BLACK
vertex & d[u] < d[v]):
– Non-tree edges (u, v) that connect a vertex
u to a descendant v in a depth first tree
• Cross edge (reaches a BLACK vertex
& d[u] > d[v]):
– Can go between vertices in same depth-first
tree (as long as there is no ancestor /
descendant relation) or between different
depth-first trees
1/ 2/7
4/5 3/6
u v w
x y z
B
F
1/8 2/7 9/
4/5 3/6
u v w
x y z
B
F
C
CSE215 27
Edge Classification (Example)
CSE215 28
Analysis of DFS(V, E)
1. for each u  V
2. do color[u] ← WHITE
3. [u] ← NIL
4. time ← 0
5. for each u  V
6. do if color[u] = WHITE
7. then DFS-VISIT(u)
(V)
(V) – exclusive
of time for
DFS-VISIT
CSE215 29
Analysis of DFS-VISIT(u)
1. color[u] ← GRAY
2. time ← time+1
3. d[u] ← time
4. for each v  Adj[u]
5. do if color[v] = WHITE
6. then [v] ← u
7. DFS-VISIT(v)
8. color[u] ← BLACK
9. time ← time + 1
10. f[u] ← time
Each loop takes
|Adj[v]|
DFS-VISIT is called exactly
once for each vertex
Total: ΣvV |Adj[v]| + (V) =
(E)
(V + E)
CSE215 30
Application of DFS
• Connected Components.
• Detecting Cycle
– Finding Back Edge
• Finding Articulation Vertex or cut node
CSE215 31
Topological Sort
Topological sort of a directed acyclic graph G =
(V, E): a linear order of vertices such that if there
exists an edge (u, v), then u appears before v in
the ordering.
• Directed acyclic graphs (DAGs)
– Used to represent precedence of events or processes
that have a partial order
a before b b before c
b before c a before c
a before c
What about
a and b?
Topological sort helps us establish a total order
CSE215 32
Topological Sort
undershorts
pants
belt
socks
shoes
watch
shirt
tie
jacket
jacket
tie
belt
shirt
watch
shoes
pants
undershorts
socks
Topological sort:
an ordering of vertices along a
horizontal line so that all directed
edges go from left to right.
CSE215 33
Topological Sort
undershorts
pants
belt
socks
shoes
watch
shirt
tie
jacket
TOPOLOGICAL-SORT(V, E)
1. Call DFS(V, E) to compute
finishing times f[v] for each
vertex v
2. When each vertex is finished,
insert it onto the front of a
linked list
3. Return the linked list of
vertices
1/
2/
3/4
5
6/7
8
9/10
11/
12/
13/14
15
16 17/18
jacket
tie
belt
shirt
watch
shoes
pants
undershorts
socks
Running time: (V + E)
CSE215 34
Topological Sort
CSE215 35
Strongly Connected Components
Given directed graph G = (V, E):
A strongly connected component (SCC) of G
is a maximal set of vertices C  V such that for
every pair of vertices u, v  C, we have both
u  v and v  u.
CSE215 36
The Transpose of a Graph
• GT = transpose of G
– GT is G with all edges reversed
– GT = (V, ET), ET = {(u, v) : (v, u)  E}
• If using adjacency lists: we can create GT in
(V + E) time
1 2
5 4
3
1 2
5 4
3
CSE215 37
Finding the SCC
• Observation: G and GT have the same SCC’s
– u and v are reachable from each other in G  they are
reachable from each other in GT
• Idea for computing the SCC of a DAG G = (V, E):
– Make two depth first searches: one on G and one on GT
1 2
5 4
3
1 2
5 4
3
CSE215 38
STRONGLY-CONNECTED-COMPONENTS(G)
1. call DFS(G) to compute finishing times f[u] for
each vertex u
2. compute GT
3. call DFS(GT), but in the main loop of DFS,
consider vertices in order of decreasing f[u]
(as computed in first DFS)
4. output the vertices in each tree of the depth-
first forest formed in second DFS as a separate
SCC
CSE215 39
Example
a b c d
e f g h
1/
2/
3/4 6
5/
7
8/
11/
12/
13/ 9
10
14
15
16
f
4
h
6
g
7
d
9
c
10
a
14
e
15
b
16
DFS on the initial graph G
DFS on GT:
• start at b: visit a, e
• start at c: visit d
• start at g: visit f
• start at h
Strongly connected components: C1 = {a, b, e}, C2 = {c, d}, C3 = {f, g}, C4 = {h}
a b c d
e f g h

More Related Content

Similar to Elementary Graph Algo.ppt

lecture 18
lecture 18lecture 18
lecture 18sajinsc
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first searchHossain Md Shakhawat
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmMSA Technosoft
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxbashirabdullah789
 
Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Deepak John
 
Topological sort
Topological sortTopological sort
Topological sortjabishah
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure Ankit Kumar Singh
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptprithivr1
 
Week12 graph
Week12   graph Week12   graph
Week12 graph IIUM
 

Similar to Elementary Graph Algo.ppt (20)

lecture 18
lecture 18lecture 18
lecture 18
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
lecture24.ppt
lecture24.pptlecture24.ppt
lecture24.ppt
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal Algorithm
 
kumattt).pptx
kumattt).pptxkumattt).pptx
kumattt).pptx
 
Graps 2
Graps 2Graps 2
Graps 2
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3
 
Topological sort
Topological sortTopological sort
Topological sort
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
 
Graph representation
Graph representationGraph representation
Graph representation
 
Graph 02
Graph 02Graph 02
Graph 02
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 

Recently uploaded

Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Recently uploaded (20)

Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

Elementary Graph Algo.ppt

  • 2. CSE215 2 Graphs - Background Graphs = a set of nodes (vertices) with edges (links) between them. Notations: • G = (V, E) - graph • V = set of vertices V = n • E = set of edges E = m 1 2 3 4 1 2 3 4 Directed graph Undirected graph 1 2 3 4 Acyclic graph
  • 3. CSE215 3 Graph Representation • Adjacency list representation of G = (V, E) – An array of V lists, one for each vertex in V – Each list Adj[u] contains all the vertices v such that there is an edge between u and v • Adj[u] contains the vertices adjacent to u (in arbitrary order) – Can be used for both directed and undirected graphs 1 2 5 4 3 2 5 / 1 5 3 4 / 1 2 3 4 5 2 4 2 5 3 / 4 1 2 Undirected graph
  • 4. CSE215 4 Graph Representation • Adjacency matrix representation of G = (V, E) – Assume vertices are numbered 1, 2, … V  – The representation consists of a matrix A V x V : – aij = 1 if (i, j)  E 0 otherwise 1 2 5 4 3 Undirected graph 1 2 3 4 5 1 2 3 4 5 0 1 1 0 0 1 1 1 1 0 1 1 0 0 0 1 1 1 0 0 1 1 1 0 0 For undirected graphs matrix A is symmetric: aij = aji A = AT
  • 5. CSE215 5 Searching in a Graph • Graph searching = systematically follow the edges of the graph so as to visit the vertices of the graph • Two basic graph searching algorithms: – Breadth-first search – Depth-first search – The difference between them is in the order in which they explore the unvisited edges of the graph • Graph algorithms are typically elaborations of the basic graph-searching algorithms
  • 6. CSE215 6 Breadth-First Search (BFS) • Input: – A graph G = (V, E) (directed or undirected) – A source vertex s  V • Goal: – Explore the edges of G to “discover” every vertex reachable from s, taking the ones closest to s first • Output: – d[v] = distance (smallest # of edges) from s to v, for all v  V – A “breadth-first tree” rooted at s that contains all reachable vertices
  • 7. CSE215 7 Breadth-First Search (cont.) • Discover vertices in increasing order of distance from the source s – search in breadth not depth – Find all vertices at 1 edge from s, then all vertices at 2 edges from s, and so on 1 2 5 4 3 7 7 9 11 12 6
  • 8. CSE215 8 Breadth-First Search (cont.) • Keeping track of progress: – Color each vertex in either white, gray or black – Initially, all vertices are white – When being discovered a vertex becomes gray – After discovering all its adjacent vertices the node becomes black – Use FIFO queue Q to maintain the set of gray vertices 1 2 5 4 3 1 2 5 4 3 source 1 2 5 4 3
  • 9. CSE215 9 Breadth-First Tree • BFS constructs a breadth-first tree – Initially contains the root (source vertex s) – When vertex v is discovered while scanning the adjacency list of a vertex u  vertex v and edge (u, v) are added to the tree – u is the predecessor (parent) of v in the breadth-first tree – A vertex is discovered only once  it has at most one parent 1 2 5 4 3 source
  • 10. CSE215 10 BFS Additional Data Structures • G = (V, E) represented using adjacency lists • color[u] – the color of the vertex for all u  V • [u] – predecessor of u – If u = s (root) or node u has not yet been discovered  [u] = NIL • d[u] – the distance from the source s to vertex u • Use a FIFO queue Q to maintain the set of gray vertices 1 2 5 4 3 d=1 =1 d=1 =1 d=2 =5 d=2 =2 source
  • 11. CSE215 11 BFS(V, E, s) 1. for each u  V - {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   9. Q ← ENQUEUE(Q, s) Q: s  0       r s t u v w x y        r s t u v w x y r s t u v w x y
  • 12. CSE215 12 BFS(V, E, s) 10. while Q   11. do u ← DEQUEUE(Q) 12. for each v  Adj[u] 13. do if color[v] = WHITE 14. then color[v] = GRAY 15. d[v] ← d[u] + 1 16. [v] = u 17. ENQUEUE(Q, v) 18. color[u]  BLACK  0    1   r s t u v w x y Q: w Q: s  0       r s t u v w x y 1 0    1   r s t u v w x y Q: w, r
  • 13. CSE215 13 Example 1 0    1   r s t u v w x y Q: s  0       r s t u v w x y Q: w, r v w x y 1 0 2   1 2  r s t u Q: r, t, x 1 0 2  2 1 2  r s t u v w x y Q: t, x, v 1 0 2 3 2 1 2  r s t u v w x y Q: x, v, u 1 0 2 3 2 1 2 3 r s t u v w x y Q: v, u, y 1 0 2 3 2 1 2 3 r s t u v w x y Q: u, y 1 0 2 3 2 1 2 3 r s t u v w x y Q: y r s t u 1 0 2 3 2 1 2 3 v w x y Q: 
  • 14. CSE215 14 Analysis of BFS 1. for each u  V - {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   9. Q ← ENQUEUE(Q, s) O(V) (1)
  • 15. CSE215 15 Analysis of BFS 10. while Q   11. do u ← DEQUEUE(Q) 12. for each v  Adj[u] 13. do if color[v] = WHITE 14. then color[v] = GRAY 15. d[v] ← d[u] + 1 16. [v] = u 17. ENQUEUE(Q, v) 18. color[u]  BLACK (1) (1) Scan Adj[u] for all vertices in the graph • Each vertex is scanned only once, when the vertex is dequeued • Sum of lengths of all adjacency lists = (E) • Scanning operations: O(E) • Total running time for BFS = O(V + E)
  • 16. CSE215 16 Shortest Paths Property • BFS finds the shortest-path distance from the source vertex s  V to each node in the graph • Shortest-path distance = (s, u) – Minimum number of edges in any path from s to u r s t u 1 0 2 3 2 1 2 3 v w x y source
  • 17. CSE215 17 Applications of BFS • Connected Components – More than one source • Two Coloring Graphs – Bipartite matching • Finding Shortest Path
  • 18. CSE215 18 Depth-First Search • Input: – G = (V, E) (No source vertex given!) • Goal: – Explore the edges of G to “discover” every vertex in V starting at the most current visited node – Search may be repeated from multiple sources • Output: – 2 timestamps on each vertex: • d[v] = discovery time • f[v] = finishing time (done with examining v’s adjacency list) – Depth-first forest 1 2 5 4 3
  • 19. CSE215 19 Depth-First Search • Search “deeper” in the graph whenever possible • Edges are explored out of the most recently discovered vertex v that still has unexplored edges • After all edges of v have been explored, the search “backtracks” from the parent of v • The process continues until all vertices reachable from the original source have been discovered • If undiscovered vertices remain, choose one of them as a new source and repeat the search from that vertex • DFS creates a “depth-first forest” 1 2 5 4 3
  • 20. CSE215 20 DFS Additional Data Structures • Global variable: time-step – Incremented when nodes are discovered/finished • color[u] – similar to BFS – White before discovery, gray while processing and black when finished processing • [u] – predecessor of u • d[u], f[u] – discovery and finish times GRAY WHITE BLACK 0 2V d[u] f[u] 1 ≤ d[u] < f [u] ≤ 2 |V|
  • 21. CSE215 21 DFS(V, E) 1. for each u  V 2. do color[u] ← WHITE 3. [u] ← NIL 4. time ← 0 5. for each u  V 6. do if color[u] = WHITE 7. then DFS-VISIT(u) • Every time DFS-VISIT(u) is called, u becomes the root of a new tree in the depth-first forest u v w x y z
  • 22. CSE215 22 DFS-VISIT(u) 1. color[u] ← GRAY 2. time ← time+1 3. d[u] ← time 4. for each v  Adj[u] 5. do if color[v] = WHITE 6. then [v] ← u 7. DFS-VISIT(v) 8. color[u] ← BLACK 9. time ← time + 1 10. f[u] ← time 1/ u v w x y z u v w x y z time = 1 1/ 2/ u v w x y z
  • 23. CSE215 23 Example 1/ 2/ u v w x y z 1/ u v w x y z 1/ 2/ 3/ u v w x y z 1/ 2/ 4/ 3/ u v w x y z 1/ 2/ 4/ 3/ u v w x y z B 1/ 2/ 4/5 3/ u v w x y z B 1/ 2/ 4/5 3/6 u v w x y z B 1/ 2/7 4/5 3/6 u v w x y z B 1/ 2/7 4/5 3/6 u v w x y z B F
  • 24. CSE215 24 Example (cont.) 1/8 2/7 4/5 3/6 u v w x y z B F 1/8 2/7 9/ 4/5 3/6 u v w x y z B F 1/8 2/7 9/ 4/5 3/6 u v w x y z B F C 1/8 2/7 9/ 4/5 3/6 10/ u v w x y z B F C 1/8 2/7 9/ 4/5 3/6 10/ u v w x y z B F C B 1/8 2/7 9/ 4/5 3/6 10/11 u v w x y z B F C B 1/8 2/7 9/12 4/5 3/6 10/11 u v w x y z B F C B The results of DFS may depend on: • The order in which nodes are explored in procedure DFS • The order in which the neighbors of a vertex are visited in DFS-VISIT
  • 25. CSE215 25 Edge Classification • Tree edge (reaches a WHITE vertex): – (u, v) is a tree edge if v was first discovered by exploring edge (u, v) • Back edge (reaches a GRAY vertex): – (u, v), connecting a vertex u to an ancestor v in a depth first tree – Self loops (in directed graphs) are also back edges 1/ u v w x y z 1/ 2/ 4/ 3/ u v w x y z B
  • 26. CSE215 26 Edge Classification • Forward edge (reaches a BLACK vertex & d[u] < d[v]): – Non-tree edges (u, v) that connect a vertex u to a descendant v in a depth first tree • Cross edge (reaches a BLACK vertex & d[u] > d[v]): – Can go between vertices in same depth-first tree (as long as there is no ancestor / descendant relation) or between different depth-first trees 1/ 2/7 4/5 3/6 u v w x y z B F 1/8 2/7 9/ 4/5 3/6 u v w x y z B F C
  • 28. CSE215 28 Analysis of DFS(V, E) 1. for each u  V 2. do color[u] ← WHITE 3. [u] ← NIL 4. time ← 0 5. for each u  V 6. do if color[u] = WHITE 7. then DFS-VISIT(u) (V) (V) – exclusive of time for DFS-VISIT
  • 29. CSE215 29 Analysis of DFS-VISIT(u) 1. color[u] ← GRAY 2. time ← time+1 3. d[u] ← time 4. for each v  Adj[u] 5. do if color[v] = WHITE 6. then [v] ← u 7. DFS-VISIT(v) 8. color[u] ← BLACK 9. time ← time + 1 10. f[u] ← time Each loop takes |Adj[v]| DFS-VISIT is called exactly once for each vertex Total: ΣvV |Adj[v]| + (V) = (E) (V + E)
  • 30. CSE215 30 Application of DFS • Connected Components. • Detecting Cycle – Finding Back Edge • Finding Articulation Vertex or cut node
  • 31. CSE215 31 Topological Sort Topological sort of a directed acyclic graph G = (V, E): a linear order of vertices such that if there exists an edge (u, v), then u appears before v in the ordering. • Directed acyclic graphs (DAGs) – Used to represent precedence of events or processes that have a partial order a before b b before c b before c a before c a before c What about a and b? Topological sort helps us establish a total order
  • 32. CSE215 32 Topological Sort undershorts pants belt socks shoes watch shirt tie jacket jacket tie belt shirt watch shoes pants undershorts socks Topological sort: an ordering of vertices along a horizontal line so that all directed edges go from left to right.
  • 33. CSE215 33 Topological Sort undershorts pants belt socks shoes watch shirt tie jacket TOPOLOGICAL-SORT(V, E) 1. Call DFS(V, E) to compute finishing times f[v] for each vertex v 2. When each vertex is finished, insert it onto the front of a linked list 3. Return the linked list of vertices 1/ 2/ 3/4 5 6/7 8 9/10 11/ 12/ 13/14 15 16 17/18 jacket tie belt shirt watch shoes pants undershorts socks Running time: (V + E)
  • 35. CSE215 35 Strongly Connected Components Given directed graph G = (V, E): A strongly connected component (SCC) of G is a maximal set of vertices C  V such that for every pair of vertices u, v  C, we have both u  v and v  u.
  • 36. CSE215 36 The Transpose of a Graph • GT = transpose of G – GT is G with all edges reversed – GT = (V, ET), ET = {(u, v) : (v, u)  E} • If using adjacency lists: we can create GT in (V + E) time 1 2 5 4 3 1 2 5 4 3
  • 37. CSE215 37 Finding the SCC • Observation: G and GT have the same SCC’s – u and v are reachable from each other in G  they are reachable from each other in GT • Idea for computing the SCC of a DAG G = (V, E): – Make two depth first searches: one on G and one on GT 1 2 5 4 3 1 2 5 4 3
  • 38. CSE215 38 STRONGLY-CONNECTED-COMPONENTS(G) 1. call DFS(G) to compute finishing times f[u] for each vertex u 2. compute GT 3. call DFS(GT), but in the main loop of DFS, consider vertices in order of decreasing f[u] (as computed in first DFS) 4. output the vertices in each tree of the depth- first forest formed in second DFS as a separate SCC
  • 39. CSE215 39 Example a b c d e f g h 1/ 2/ 3/4 6 5/ 7 8/ 11/ 12/ 13/ 9 10 14 15 16 f 4 h 6 g 7 d 9 c 10 a 14 e 15 b 16 DFS on the initial graph G DFS on GT: • start at b: visit a, e • start at c: visit d • start at g: visit f • start at h Strongly connected components: C1 = {a, b, e}, C2 = {c, d}, C3 = {f, g}, C4 = {h} a b c d e f g h