Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan,...
Transcript of Data Structures and Algorithms · Session: Basic Operations on Graphs (Program) Ajit A. Diwan,...
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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;
}}
}
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
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
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
IIT Bombay
Thank you
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 19