0% found this document useful (0 votes)
22 views

Graph Searching - Java

The document discusses breadth-first search (BFS) and depth-first search (DFS) algorithms for traversing graphs. It provides pseudocode for BFS and DFS and explains how each works. BFS uses a queue to explore all nodes at each level starting from the source node, finding the shortest paths. DFS uses a stack and explores nodes as deep as possible along each branch before backtracking, and can get stuck in cycles. The document compares key aspects of BFS and DFS like memory usage, predictability, and suitability for different graph and problem types.

Uploaded by

onlineearningfor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Graph Searching - Java

The document discusses breadth-first search (BFS) and depth-first search (DFS) algorithms for traversing graphs. It provides pseudocode for BFS and DFS and explains how each works. BFS uses a queue to explore all nodes at each level starting from the source node, finding the shortest paths. DFS uses a stack and explores nodes as deep as possible along each branch before backtracking, and can get stuck in cycles. The document compares key aspects of BFS and DFS like memory usage, predictability, and suitability for different graph and problem types.

Uploaded by

onlineearningfor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

(JAVA)

Hira Awais
Lecturer (DCS)
DDSA (FOC)
 Slide courtesy: Dr. Zahid Halim
 Given: a graph G = (V, E), directed or undirected
 Goal: methodically explore every vertex and every edge
 Ultimately: build a tree on the graph
 Pick a vertex as the root
 Choose certain edges to produce a tree
 Given a G=(V,E) and distinguished source vertex s, BFS systematically
explores the edges of G to “discover” every vertex reachable from s.

 Creates a BFS tree rooted at s that contains all such vertices.

 Expands the frontier between discovered and undiscovered vertices


uniformly across the breadth of the frontier.

 The algorithm discovers all vertices at distance k from s before discovering


any vertices at distance k+1
 will associate vertex “colors” to guide the algorithm
 White vertices have not been discovered
▪ All vertices start out white

 Grey vertices are discovered but not fully explored


▪ They may be adjacent to white vertices and represent the frontier between the discovered and the
undiscovered.

 Black vertices are discovered and fully explored


▪ They are adjacent only to black and gray vertices
 Explore vertices by scanning adjacency list of grey vertices
1. BFS(G, s) {
2. initialize vertices;
3. Q = {s}; // Q is a queue initialize to s
4. while (Q not empty) {
5. u = Dequeue(Q);
6. for each v  u->adj {
7. if (v->color == WHITE){
8. v->color = GREY;
9. v->d = u->d + 1;
10. v->p = u;
11. Enqueue(Q, v);
12. }
13. }
14. u->color = BLACK;
15. }
16. }
r s t u

   

   
v w x y
r s t u

 0  

   
v w x y

Q: s
r s t u

1 0  

 1  
v w x y

Q: w r
r s t u

1 0 2 

 1 2 
v w x y

Q: r t x
r s t u

1 0 2 

2 1 2 
v w x y

Q: t x v
r s t u

1 0 2 3

2 1 2 
v w x y

Q: x v u
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: v u y
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: u y
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: y
r s t u

1 0 2 3

2 1 2 3
v w x y

Q: Ø
 BFS calculates the shortest-path distance to the source node
 Shortest-path distance (s,v) = minimum number of edges from s to
v, or  if v not reachable from s
 BFS builds breadth-first tree, in which paths to root represent
shortest paths in G
 Thus can use BFS to calculate shortest path from one vertex to
another in O(V+E) time
 Web Crawlers
 Search engines or web crawlers can easily build multiple levels of indexes by
employing BFS. BFS implementation starts from the source, which is the web
page, and then it visits all the links from that source.

 P2P Networks
 BFS can be implemented to locate all the nearest or neighboring node in a P2P
network. This will find the required data faster.

 Un-weighted graph
 BFS can easily create the shortest path and minimum spanning tree to visit all
the vertices of the graph in the shortest possible time with high accuracy.
 Depth-first search: Strategy Go as deep as can visiting un-visited
nodes

 Choose any un-visited vertex when you have a choice

 When stuck at a dead-end, backtrack as little as possible back up


to where you could go to another unvisited vertex

 Then continue to go on from that point

 Eventually you’ll return to where you started


DFS(G)
for each vertex u V[G]{
color[u]=white
parent[u]=NULL
}
time=0
for each vertex u V[G]{
if color[u]=white then
DFS-VISIT(u)
}

DFS-VISIT(u)
color[u]=GRAY
time=time+1
d[u]=time
for each vertex v  adj[u] {
if color[v]=white Then
parent[v]=u
DFS-VISIT(v)
}
color[u]=black
f[u]=time=time+1
1. Initialize all nodes to the ready state (STATUS=1)
2. PUSH starting node A on stack and change its status to waiting(STATUS=2)
3. Repeat 4-5 until stack is empty
4. POP N. Process it and change its status to processed(STATUS=3)
5. PUSH on stack all the neighbors of N that are still in ready state and
change their status to waiting
6. Exit
 Path finding
 We can specialize in DFS algorithm to search a path between two
vertices

 Detecting a cycle in a graph


 A graph has a cycle if we found a back edge during DFS. Therefore,
we should run DFS for the graph and verify for back edges.

 Weighted graph
 DFS traversal generates the shortest path tree and minimum
spanning tree
Breadth-First Search Depth-First Search

Find the shortest path to the destination or closet node Finds the longest path or the farthest node to the
to the starting node starting node

Uses queue to keep track of the next location to visit Uses stack to keep track of the next location to visit

Traverses according to tree level Traverses according to tree depth

Implement using FIFO list Implement using LIFO list

Require more memory than DFS Requires less memory than BFS

Algorithm gives shallowest path solution Does not guarantee shallowest path solution

Can never trap in infinite loop Can trap in infinite loop

No need of back tracking Back tracking is needed


BFS is more memory-intensive than DFS, as it needs to store all
the nodes at each level, while DFS only needs to store the
current path
 BFS is more likely to find a solution faster than DFS, if there is
one, as it explores all possible paths at each level while DFS
may get stuck in a dead end or a cycle.

 BFS is more predictable than DFS, as it always visits the nodes


in the same order, while DFS may vary depending on the order
of edges.
 Structure and size of the graph
 Location & number of the nodes of interest
 Type and complexity of the problem
 Memory & time constraint

You might also like