data structures and algorithms · session: basic operations on graphs (program) ajit a. diwan,...

19
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: Basic Operations on Graphs (Program) 1 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Upload: others

Post on 05-Jul-2020

3 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Data Structures and AlgorithmsProf. Ajit A. Diwan

Prof. Ganesh RamakrishnanProf. Deepak B. Phatak

Department of Computer Science and EngineeringIIT Bombay

Session: Basic Operations on Graphs (Program)

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

Page 2: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Data Structures and Functions

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

• 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

Page 3: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Data Structures and Functions

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

• 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

Page 4: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Representation of Vector

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

0

1

2

n-1

List 1

List 2

List 3

List n

vectorListIndex

Page 5: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Representation of Vector

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

0

1

2

n-1

Node 1, Destination nodes from Node 1

vectorList

Node 2, Destination nodes from Node 2

Node 3, Destination nodes from Node 3

Node n, Destination nodes from Node n

Index

Page 6: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Program

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

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

Page 7: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Example: Creating Graph Nodes

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

0

1

2

3

4

A

vectorList

B

C

D

E

Index

• Nodes A, B, C, D, and E are created• Its destination nodes are not known as of now

Page 8: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Program

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

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

Page 9: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Example: Adding an Edge

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

• Add edge from C to A

0

1

2

3

4

A

vectorList

B

C

D

E

Index

0

1

2

3

4

A

vectorList

B

C, A

D

E

Index

Page 10: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Program

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

template<class T>void graph<T>::addEdge(T source, T destination){

//Iterate through the ‘vectorList’ for adding an edgefor(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

Page 11: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Example: Removing an Edge

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

0

1

2

3

4

A

vectorList

B

C, A

D

E

Index

• Remove edge from C to A

0

1

2

3

4

A

vectorList

B

C

D

E

Index

Page 12: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Program

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

template<class T>void graph<T>::removeEdge(T source, T destination){

//Iterate through the ‘vectorList’ for removing an edgefor(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

Page 13: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Program

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

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

Page 14: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Program

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

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

Page 15: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Program

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

//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;

}}

}

Page 16: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Program

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

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');

cout << "\nOutgoing Edges\n";g.printOutgoing();cout << "\nIncoming Edges\n";g.printIncoming(); return 0;

} //End of main

Page 17: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Exercise

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

• 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

Page 18: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Problems in Real Life

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

• 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

Page 19: Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 1. IIT Bombay Data Structures

IIT Bombay

Thank you

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