5. What is a graph?
1.Directed/Undire
cted
2.Weighted/Unwei
ghted
3.Cyclic/Acyclic
A set of vertices and edges
6. Breadth First Search (BFS)
Start several paths at a time, and advance in each one step at a time
The breadth-first search uses a FIFO queue.
Depth First Search (DFS)
Once a possible path is found, continue the search until the end of
the path
The Depth-first search uses a LIFO Stack.
Graph Traversal
7. How It Works?
1.Pick a source vertex S to start.
2.Discover the vertices that are adjacent to S.
Depth-first:
visit all neighbors of
a neighbor before
visiting your other
neighbors
Breadth First:
Pick each child of
S in turn and
discover their
vertices adjacent
to that child.
Done when all
children have been
discovered and
examined.
8. Algorithm
BFS(G, s)
for each vertex u V [G] - {s}
do color[u] ← WHITE
d[u] ← ∞
π[u] ← NIL
color[s] ← GRAY
d[s] ← 0
π[s] ← NIL
Q ← Ø
ENQUEUE(Q, s)
while Q ≠ Ø
do u ← DEQUEUE(Q)
for each v Adj[u]
do if color[v] = WHITE
then color[v] ← GRAY
d[v] ← d[u] + 1
π[v] ← u
ENQUEUE(Q, v)
color[u] ← BLACK
DFS(G)
for each vertex u V [G]
do color[u] ← WHITE
π[u] ← NIL
time ← 0
for each vertex u V [G]
do if color[u] = WHITE
then DFS-VISIT(u)
DFS-VISIT(u)
color[u] ← GRAY ▹White vertex u
has just been discovered.
time ← time +1
d[u] time
for each v Adj[u] ▹Explore edge(u,
v).
do if color[v] = WHITE
then π[v] ← u
DFS-VISIT(v)
color[u] BLACK ▹ Blacken u; it is
finished.
f [u] ▹ time ← time +1
17. Analysis of BFS
For a Graph G=(V, E) and n = |V| and
m=|E|
When Adjacency List is used
Complexity is O(m + n)
When Adjacency Matrix is used
Scanning each row for checking the
connectivity of a Vertex is in order
O(n).
So, Complexity is O(n2 )
34. A
H
B
F
E
D
C
G
Walk-Through
Visited Array
A √
B √
C √
D √
E √
F
G √
H √
No unvisited nodes adjacent to
B. Backtrack (pop the stack).
A
H
G
E
D
The order nodes are visited:
D, C, E, G, H, A, B
44. For a Graph G=(V, E) and n = |V| and m=|E|
When Adjacency List is used Complexity is O(m + n)
When Adjacency Matrix is used Scanning each row
for checking the connectivity of a Vertex is in order O(n).
So, Complexity is O(n2 )
DFS uses space O(|V|) in the worst case to store the
stack of vertices on the current search path as well as
the set of already-visited vertices.
Analysis of DFS
45. BFS:
*Testing a graph for bipartiteness
*To find the shortest path from a vertex s to a
vertex v in an unweighted graph
*To find the length of such a path
*To find out if a graph contains cycles
*To construct a BSF tree/forest from a graph
*Copying garbage collection
Applications
46. DFS:
* Finding connected components.
*Topological sorting.
*Finding the bridges of a graph.
*cycle Detecting
*Finding strongly connected components.
*Finding biconnectivity in graphs.
Applications
47. How Can Use BFS
Bipartite graphs can’t contain odd
cycles.
Problem:
Determine if a graph G is bipartite.
48. Testing Bipartiteness
How can we test if G is bipartite?
1.Do a BFS starting from some node s.
2.Color even levels “blue” and odd levels
“red.”
3.Check each edge to see if any edge has
both endpoints the same color.
51. Finding Cycles Using DFS
• Similar to using BFS.
• For undirected graphs, classify the edges
into 3 categories during program
execution:
1.unvisited edge
2.discovery edge and
3.back (cross) edge.
• If there exists a back edge, the
undirected graph contains a cycle.