Download presentation
Presentation is loading. Please wait.
1
Data Structures and Algorithms
Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Graph Traversal (Breadth-First Search) Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
2
Breadth-First Search Function 1: getVertices
Gets the vertices of the graph and stores it in an array. Called by function BFS Function 2: BFS Traverses the graph in BFS fashion List ‘s’ Used to store outgoing nodes We use the list construct of STL, as a queue Use ‘push_back’ to push the nodes in the list Use ‘pop_front’ to pop the nodes out of the list Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
3
Demonstration A B E C D Visited: A 1 Steps List ‘s’
Initial: Insert A in the list Pop A from the list A is not visited, so, visit it Push back the outgoing nodes of A in the list A B, C A B E C D 1 Visited: A Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
4
Demonstration A B E C D Visited: A, B 2 1 Steps List ‘s’
Pop B from the list B is not visited, so, visit it Push back the outgoing nodes of B in the list B, C C C, D A B E C D 1 Visited: A, B Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
5
Demonstration A B E C D Visited: A, B, C 2 1 3 Steps List ‘s’
Pop C from the list C is not visited, so, visit it Push back the outgoing nodes of C in the list C, D D D D A B E C D 1 3 Visited: A, B, C Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
6
Demonstration A B E C D Visited: A, B, C, D 2 1 4 3 Steps List ‘s’
Pop D from the list D is not visited, so, visit it Push back the outgoing nodes of D in the list D, D D D, E A B E C D 1 4 3 Visited: A, B, C, D Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
7
Demonstration A B E C D Visited: A, B, C, D 2 1 4 3 Steps List ‘s’
Pop D from the list D is already visited Now, examine the next node in the list D, E E A B E C D 1 4 3 Visited: A, B, C, D Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
8
Demonstration A B E C D Visited: A, B, C, D, E 2 1 4 5 3 Steps
List ‘s’ Pop E from the list E is not visited, so, visit it All nodes traversed successfully, as list is empty E A B E C D 1 4 5 3 Visited: A, B, C, D, E Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
9
BFS Example Nodes visited in BFS order A, B, C, D, F, E, G B A D E C F
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
10
Program template<class T> class graph{ template<class T>
vector<list<T> > vectorList; int size; public: void createGraphNodes(int a, T*); bool addEdge(T, T); bool removeEdge(T, T); void getVertices(T c[]); void BFS(T a); void printOutgoing(); void printIncoming(); }; //End of class template<class T> void graph<T>::createGraphNodes(int a, T vertex[]){ size = a; vectorList.resize(a); int arrayIndex = 0; for(typename vector<list<T> >::iterator vectorIterator = vectorList.begin(); vectorIterator != vectorList.end(); vectorIterator++, arrayIndex++){ (*vectorIterator).push_back(vertex[arrayIndex]); } } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
11
Program template<class T>
void graph<T>::getVertices(T c[]){ int index = 0; for(typename vector<list<T> >::iterator vectorIterator = vectorList.begin(); vectorIterator != vectorList.end(); vectorIterator++, index++){ typename list<T>::iterator listIterator = (*vectorIterator).begin(); c[index] = (*listIterator); } } // End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
12
Program template<class T> void graph<T>::BFS(T a){
T *c = new T[size]; getVertices(c); bool visited[size]; for(int i = 0; i < size; i++) visited[i] = false; list<T> s; s.push_back(a); while(s.size() > 0){ T node = s.front(); s.pop_front(); int positionOfNode = find(c,c+size, node) - c; if(visited[positionOfNode] == false){ //Code to perform operations if the node is not visited } // End of if that determines whether the node is visited or not } //End of while } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
13
Program //Code to perform operations if the node is not visited
visited[positionOfNode] = true; //Visit it cout << "Visiting: " << node << endl; for(typename vector<list<T> >::iterator vectorIterator = vectorList.begin(); vectorIterator != vectorList.end(); vectorIterator++){ typename list<T>::iterator listIterator = (*vectorIterator).begin(); if((*listIterator) == node){ listIterator++; for( ; listIterator != (*vectorIterator).end(); listIterator++){ s.push_back((*listIterator)); //Push all outgoing edges of the node in the list } break; } // End of for Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
14
Program int main(){ char vertex[] = {'A','B','C','D','E'}; graph<char> g; g.createGraphNodes(sizeof(vertex)/sizeof(vertex[0]),vertex); g.addEdge('A','B'); g.addEdge('A','C'); g.addEdge('B','D'); g.addEdge('C','D'); g.addEdge('D','E'); g.printOutgoing(); cout << endl; cout << endl << "Printing BFS" << endl; g.BFS('A'); return 0; } //End of main Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
15
Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.