Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R4. Disjoint Sets.
Advertisements

Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
Karsten Steinhaeuser Aaron Wenger December 09, 2003.
CS2420: Lecture 36 Vladimir Kulyukin Computer Science Department Utah State University.
Exercise Computing the cyclomatic complexity Philippe CHARMAN Last update:
14 Graph ADTs  Graph concepts.  Graph applications.  A graph ADT: requirements, contract.  Implementations of graphs: edge-set, adjacency-set, adjacency-matrix.
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
CS200 Algorithms and Data StructuresColorado State University Part 10. Graphs CS 200 Algorithms and Data Structures 1.
1 Writing a Good Program 8. Elementary Data Structure.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
CSC2100B Tutorial 10 Graph Jianye Hao.
CSCI2100 Data Structures Tutorial 12
Graph Terminologies Department of CE/IT M.S.P.V.L.Polytechnic College,
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.
2D Arrays Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Spring 2006.
Review. Problem 1 What is the output of the following piece of code void increment(int x) { x++; } int main() { int y = 10; increment(y); cout
Graph Revisited Fei Chen CSCI2100B Data Structures Tutorial 12 1.
Graphs and Shortest Paths Using ADTs and generic programming.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
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.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
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.
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Regarding homework 9 Many low grades
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
Graphs Representation, BFS, DFS
Data Structures and Algorithms
Data Structures and Algorithms
Graphs.
Programming Abstractions
Algorithms and Data Structures
Data Structures and Algorithms
Algorithms and Data Structures
Data Structures and Algorithms
Chapter 4 Loops Case Studies
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Data Structures and Algorithms
What remains Topics Assignments Final exam
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
CS200: Algorithm Analysis
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Graphs Chapter 11 Objectives Upon completion you will be able to:
Introduction to Programming
Shortest-Paths Trees Kun-Mao Chao (趙坤茂)
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Programming Abstractions
CSE 214 – Computer Science II Graph Walking
Graphs.
Containers: Queue and List
Fundamental Structures of Computer Science II
Graphs.
Podcast Ch26b Title: Digraph Class Implementation
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Chapter 3 Lists, Stacks, and Queues
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
Graphs G = (V,E) V is the vertex set.
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty
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: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Data Structures and Functions Vector ‘vectorList’ Vector which is of type ‘list’ Function 1: ‘createGraphNodes’ Creates nodes of graph i.e. A, B, C, etc. Function 2: ‘addEdge’ Adds an edge from one node to another node of the graph Function 3: ‘removeEdge’ Removes an existing edge from one node to another node of the graph Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Data Structures and Functions Function 4: ‘printOutgoing’ Displays all outgoing edges for each node in the graph Function 5: ‘printIncoming’ Displays all incoming edges for each node in the graph Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Representation of Vector Index vectorList 1 2 … n-1 List 1 List 2 List 3 … List n Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Representation of Vector Index vectorList 1 2 … n-1 Node 1, Destination nodes from Node 1 Node 2, Destination nodes from Node 2 Node 3, Destination nodes from Node 3 … Node n, Destination nodes from Node n Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program template<class T> class graph{ vector<list<T> > vectorList; public: void createGraphNodes(int a, T*); void addEdge(T, T); void removeEdge(T, T); void printOutgoing(); void printIncoming(); }; //End of class Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Example: Creating Graph Nodes Nodes A, B, C, D, and E are created Its destination nodes are not known as of now Index vectorList 1 2 3 4 A B C D E Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program template<class T> void graph<T>::createGraphNodes(int a, T vertex[]){ 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

Example: Adding an Edge Add edge from C to A Index vectorList Index vectorList 1 2 3 4 1 2 3 4 A A B B C C, A D D E E Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program template<class T> void graph<T>::addEdge(T source, T destination){ //Iterate through the ‘vectorList’ for adding an edge for(typename vector<list<T> >::iterator vectorIterator = vectorList.begin(); vectorIterator != vectorList.end(); vectorIterator++){ typename list<T>::iterator listIterator = (*vectorIterator).begin(); if ( (*listIterator) == source ) { //source node found (*vectorIterator).push_back(destination); //Edge added } } //End of for } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Example: Removing an Edge Remove edge from C to A Index vectorList Index vectorList 1 2 3 4 1 2 3 4 A A B B C, A C D D E E Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program template<class T> void graph<T>::removeEdge(T source, T destination){ //Iterate through the ‘vectorList’ for removing an edge for(typename vector<list<T> >::iterator vectorIterator = vectorList.begin(); vectorIterator != vectorList.end(); vectorIterator++){ typename list<T>::iterator listIterator = (*vectorIterator).begin(); if ( (*listIterator) == source ) { //source node found (*vectorIterator).remove(destination); //Edge removed } } //End of for } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program template<class T> void graph<T>::printOutgoing(){ for(typename vector<list<T> >::iterator vectorIterator = vectorList.begin(); vectorIterator != vectorList.end(); vectorIterator++){ typename list<T>::iterator listIterator = (*vectorIterator).begin(); cout << (*listIterator) << ": "; listIterator++; for( ; listIterator != (*vectorIterator).end(); listIterator++){ cout << (*listIterator) << " "; } cout << endl; } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program template<class T> void graph<T>::printIncoming(){ //Iterate for All Nodes (Incoming) for(typename vector<list<T> >::iterator vIt = vectorList.begin(); vIt != vectorList.end(); vIt++){ typename list<T>::iterator lIt = (*vIt).begin(); cout << (*lIt) << ": "; //Code to examine all nodes starting with A, and display incoming edge(s) for each cout << endl; } } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program //Code to examine all nodes starting from A and display incoming edge(s) for(typename vector<list<T> >::iterator vectorIterator = vectorList.begin(); vectorIterator != vectorList.end(); vectorIterator++){ typename list<T>::iterator listIterator = (*vectorIterator).begin(); listIterator++; for( ; listIterator != (*vectorIterator).end(); listIterator++){ if (*listIterator == *lIt) { cout << (*vectorIterator).front() << " "; break; } Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Program int main(){ int size = 5; char vertex[] = {'A','B','C','D','E'}; graph<char> g; g.createGraphNodes(5,vertex); cout << "\nCreating graph nodes\n\n"; g.addEdge('A','B'); g.addEdge('A','C'); g.addEdge('C','A'); g.addEdge('C','D'); g.addEdge('C','B'); g.addEdge('D','B'); cout << "\nOutgoing Edges\n"; g.printOutgoing(); cout << "\nIncoming Edges\n"; g.printIncoming(); cout << endl; g.removeEdge('A','B'); g.removeEdge('C','D'); return 0; } //End of main Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Exercise The program discussed implements a directed graph. Modify it to implement an undirected graph. Write functions to: Determine whether an edge exists from one node to another Determine the ‘Outdegree’ of a node Determine the ‘Indegree’ of a node Remove an existing node from the graph Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Problems in Real Life In real life, various errors can occur Adding an edge which exists Adding an edge where either the ‘Source’ node or ‘Destination’ node is not present in the graph Removing an edge which is not present Removing an edge where either the ‘Source’ node or ‘Destination’ node is not present in the graph Adding a node to the graph, which exists Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

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