Presentation is loading. Please wait.

Presentation is loading. Please wait.

IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.

Similar presentations


Presentation on theme: "IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering."— Presentation transcript:

1 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 1Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

2 IIT Bombay Depth-First Search Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay2 Function 1: getVertices 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

3 IIT Bombay Demonstration Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay3 A B E C D StepsList ‘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 1 Visited: A

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

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

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

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

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

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

10 IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay10 template class graph{ vector > vectorList; int size; public: void createGraphNodes(int a, T*); bool addEdge(T, T); bool removeEdge(T, T); void getVertices(T c[]); void DFS(T a); void printOutgoing(); void printIncoming(); }; //End of class template void graph ::createGraphNodes(int a, T vertex[]){ size = a; vectorList.resize(a); int arrayIndex = 0; for(typename vector >::iterator vectorIterator = vectorList.begin(); vectorIterator != vectorList.end(); vectorIterator++, arrayIndex++){ (*vectorIterator).push_back(vertex[arrayIndex]); } } //End of function

11 IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay11 template void graph ::getVertices(T c[]){ int index = 0; for(typename vector >::iterator vectorIterator = vectorList.begin(); vectorIterator != vectorList.end(); vectorIterator++, index++){ typename list ::iterator listIterator = (*vectorIterator).begin(); c[index] = (*listIterator); } } // End of function

12 IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay12 template void graph ::DFS(T a){ T *c = new T[size]; getVertices(c); bool visited[size]; for(int i = 0; i < size; i++) visited[i] = false; list s; s.push_back(a); while(s.size() > 0){ T node = s.back(); s.pop_back(); 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

13 IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay13 //Code to perform operations if the node is not visited visited[positionOfNode] = true; //Visit it cout << "Visiting: " << node << endl; for(typename vector >::iterator vectorIterator = vectorList.begin(); vectorIterator != vectorList.end(); vectorIterator++){ typename list ::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

14 IIT Bombay Program Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay14 int main(){ char vertex[] = {'A','B','C','D','E'}; graph 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 DFS" << endl; g.DFS('A'); return 0; } //End of main

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


Download ppt "IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering."

Similar presentations


Ads by Google