Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.