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

Slides:



Advertisements
Similar presentations
CSE Lectures 18 – Graphs Graphs & Characteristics
Advertisements

© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graph Traversals Depth-First Traversal. The Algorithm.
Graph Traversals Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example. –Implementation. Review.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
Graph Traversals General Traversal Algorithm Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Main Index Contents 11 Main Index Contents Week 4 – Stacks.
Search Related Algorithms. Graph Code Adjacency List Representation:
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
Backtracking Algorithm Depth-First Search Text Read Weiss, § 9.6 Depth-First Search and § 10.5 Backtracking Algorithms.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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 Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
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 Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
Searching Arrays Linear search Binary search small arrays
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Cpt S 122 – Data Structures Abstract Data Types
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
CS212: Data Structures and Algorithms
Data Structures and Algorithms
Chapter 15 Lists Objectives
Data Structures and Algorithms
Csc 2720 Instructor: Zhuojun Duan
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Linked List Yumei Huo Department of Computer Science
Data Structures and Algorithms
Graph Traversals Depth-First Traversals. Algorithms. Example.
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
More Graph Algorithms.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Chapter 9 One-Dimensional Arrays
Search Related Algorithms
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Graph Traversals Depth-First Traversals. Algorithms. Example.
Graphs Part 2 Adjacency Matrix
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Level 1 STL – Linear DS fb.com/acmicpcfcicu. Static Arrays Creating 1D, 2D, ND Static Arrays. Accessing Time. Traversing a static array with N dimensions.
COMP171 Depth-First Search.
Lists - I The List ADT.
Lists - I The List ADT.
Queues Jyh-Shing Roger Jang (張智星)
Containers: Queue and List
Visit for more Learning Resources
Depth-First Search CSE 2011 Winter April 2019.
Depth-First Search CSE 2011 Winter April 2019.
List Iterator Implementation
Object-Oriented Programming (OOP) Lecture No. 34
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Chapter 3 Lists, Stacks, and Queues
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Graph Traversals Depth-First Traversals. Algorithms. Example.
Presentation transcript:

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

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

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

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

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 Visited: A, C, D

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 Visited: A, C, D, E

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 Visited: A, C, D, E, B

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 Visited: A, C, D, E, B

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

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

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

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

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

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

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