We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1
Algorithm BFS(Graph, source):
Input: Graph represented as an adjacency list, starting vertex 'source'
Output: BFS Traversal of the graph from 'source'
1. Create a queue (queue)
2. Initialize a visited array of size (number of vertices) to false 3. Mark source as visited 4. Enqueue source into queue
5. While queue is not empty:
a. Dequeue vertex 'v' from queue b. Print vertex 'v' c. For each adjacent vertex 'e' of 'v' in adjacency list: i. If 'e' is not visited: - Mark 'e' as visited - Enqueue 'e' into the queue
End While
DFS 1. Initialize stack as empty. 2. Initialize visited[] array of size V with all false. 3. Push the starting vertex 'start' onto the stack.
4. WHILE stack is not empty:
a. Pop a vertex 'vertex' from the stack. b. IF vertex is not visited: i. Mark vertex as visited. ii. Print vertex. c. FOR each neighbor 'neighbor' of vertex in adjacency list: IF neighbor is not visited: Push neighbor onto the stack.