IIT Bombay Data Structures and Algorithms Prof Ajit

IIT Bombay 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 1

Breadth-First Search IIT Bombay • Function 1: get. Vertices • 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 2

Demonstration IIT Bombay Steps • Initial: Insert A in the list B D 1 A E List ‘s’ A • Pop A from the list • A is not visited, so, visit it C • Push back the outgoing nodes of A in the list B, C Visited: A Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 3

Demonstration IIT Bombay 2 Steps B D 1 A E C List ‘s’ B, C • Pop B from the list C • B is not visited, so, visit it C • Push back the outgoing nodes of B in the list C, D Visited: A, B Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 4

Demonstration IIT Bombay 2 Steps B D 1 A E C 3 List ‘s’ C, D • Pop C from the list D • C is not visited, so, visit it D • Push back the outgoing nodes of C in the list DD Visited: A, B, C Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 5

Demonstration IIT Bombay 2 Steps B D 1 A 4 E C 3 List ‘s’ D, D • Pop D from the list D • D is not visited, so, visit it D • Push back the outgoing nodes of D in the list D, E Visited: A, B, C, D Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 6

Demonstration IIT Bombay 2 Steps B D 1 A 4 E C 3 List ‘s’ D, E • Pop D from the list E • D is already visited E • Now, examine the next node in the list E Visited: A, B, C, D Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 7

Demonstration IIT Bombay 2 Steps B D 1 A 4 E 5 C 3 Visited: A, B, C, D, E Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay List ‘s’ E • Pop E from the list • E is not visited, so, visit it • All nodes traversed successfully, as list is empty 8

BFS Example IIT Bombay Nodes visited in BFS order A, B, C, D, F, E, G B A D E F G C Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 9

Program IIT Bombay template<class T> class graph{ vector<list<T> > vector. List; int size; public: void create. Graph. Nodes(int a, T*); bool add. Edge(T, T); bool remove. Edge(T, T); void get. Vertices(T c[]); void BFS(T a); void print. Outgoing(); void print. Incoming(); }; //End of class template<class T> void graph<T>: : create. Graph. Nodes(int a, T vertex[]){ size = a; vector. List. resize(a); int array. Index = 0; for(typename vector<list<T> >: : iterator vector. Iterator = vector. List. begin(); vector. Iterator != vector. List. end(); vector. Iterator++, array. Index++){ (*vector. Iterator). push_back(vertex[array. Index]); } } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 10
![Program IIT Bombay template<class T> void graph<T>: : get. Vertices(T c[]){ int index = Program IIT Bombay template<class T> void graph<T>: : get. Vertices(T c[]){ int index =](http://slidetodoc.com/presentation_image_h2/8177f3b4bae8c09d799ea4a6f2b3cffc/image-11.jpg)
Program IIT Bombay template<class T> void graph<T>: : get. Vertices(T c[]){ int index = 0; for(typename vector<list<T> >: : iterator vector. Iterator = vector. List. begin(); vector. Iterator != vector. List. end(); vector. Iterator++, index++){ typename list<T>: : iterator list. Iterator = (*vector. Iterator). begin(); c[index] = (*list. Iterator); } } // End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 11

Program IIT Bombay template<class T> void graph<T>: : BFS(T a){ T *c = new T[size]; get. Vertices(c); for(int i = 0; i < size; i++) visited[i] = false; list<T> s; s. push_back(a); bool visited[size]; while(s. size() > 0){ T node = s. front(); s. pop_front(); int position. Of. Node = find(c, c+size, node) - c; if(visited[position. Of. Node] == 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 12

Program IIT Bombay //Code to perform operations if the node is not visited[position. Of. Node] = true; //Visit it cout << "Visiting: " << node << endl; for(typename vector<list<T> >: : iterator vector. Iterator = vector. List. begin(); vector. Iterator != vector. List. end(); vector. Iterator++){ typename list<T>: : iterator list. Iterator = (*vector. Iterator). begin(); if((*list. Iterator) == node){ list. Iterator++; for( ; list. Iterator != (*vector. Iterator). end(); list. Iterator++){ s. push_back((*list. Iterator)); //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 13
![Program IIT Bombay int main(){ char vertex[] = {'A', 'B', 'C', 'D', 'E'}; graph<char> Program IIT Bombay int main(){ char vertex[] = {'A', 'B', 'C', 'D', 'E'}; graph<char>](http://slidetodoc.com/presentation_image_h2/8177f3b4bae8c09d799ea4a6f2b3cffc/image-14.jpg)
Program IIT Bombay int main(){ char vertex[] = {'A', 'B', 'C', 'D', 'E'}; graph<char> g; g. create. Graph. Nodes(sizeof(vertex)/sizeof(vertex[0]), vertex); g. add. Edge('A', 'B'); g. add. Edge('A', 'C'); g. add. Edge('B', 'D'); g. add. Edge('C', 'D'); g. add. Edge('D', 'E'); g. print. Outgoing(); cout << endl << "Printing BFS" << endl; g. BFS('A'); return 0; } //End of main Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 14

IIT Bombay Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 15
- Slides: 15