Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Advertisements

CS 261 – Recitation 9 & 10 Graphs & Final review
Graph Traversals Depth-First Traversal. The Algorithm.
Graph Traversals Depth-First Traversals. –Algorithms. –Example. –Implementation. Breadth-First Traversal. –The Algorithm. –Example. –Implementation. Review.
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.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
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.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
Search Related Algorithms. Graph Code Adjacency List Representation:
Graphs – Part II CS 367 – Introduction to Data Structures.
Another dynamic program %...*..*..*% % % %*..*******% %*.*.***.*.% Q: What is the shortest path that delivers papers.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
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.
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
GRAPH TRAVERSING BY PROF. UZAIR SALMAN
Graphs A New Data Structure
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
Graph Traversals Some algorithms require that every vertex of a graph be visited exactly once. The order in which the vertices are visited may be important,
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
What remains Topics Assignments Final exam
Data Structures and Algorithms for Information Processing
Graph Traversals Depth-First Traversals. Algorithms. Example.
CSC 172 DATA STRUCTURES.
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
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
COMP171 Depth-First Search.
Queues Jyh-Shing Roger Jang (張智星)
Containers: Queue and List
Heap code in C++ template <class eType>
Depth-First Search CSE 2011 Winter April 2019.
Depth-First Search CSE 2011 Winter April 2019.
List Iterator Implementation
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Graph Search in C++ Andrew Lindsay.
CS203 Lecture 14.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Graph Traversals Depth-First Traversals. Algorithms. Example.
Data Structures & Programming
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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