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 (Depth-First Search) Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1

Depth-First Search IIT Bombay • Function 1: get. Vertices • Gets the vertices of the graph and stores it in an array. • Called by function DFS • Function 2: DFS • Traverses the graph in DFS fashion • List ‘s’ • Used to store adjacent nodes, from the current node • We use the list construct of STL, as a stack • Use ‘push_back’ to push the nodes in the list • Use ‘pop_back’ 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 Steps B D 1 A E C 2 List ‘s’ B, C • Pop C from the list B • C is not visited, so, visit it B • Push back the outgoing nodes of C in the list B, D Visited: A, C Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 4

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

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

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

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

Another DFS Example IIT Bombay Nodes visited in DFS order A, D, G, F, E, C, B 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 DFS(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/c6b45b3e1edd5ca85a1206025e991a13/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>: : DFS(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. back(); s. pop_back(); 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/c6b45b3e1edd5ca85a1206025e991a13/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 DFS" << endl; g. DFS('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