8. graph - data structures using c++ by varsha patil
TRANSCRIPT
![Page 1: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/1.jpg)
1Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
8.GRAPHS
![Page 2: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/2.jpg)
2Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Objectives Graphs as one of the most important non-
linear data structures
The representation that models various kinds of graphs
Some useful graph algorithms
![Page 3: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/3.jpg)
3Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Introtuction A graph G is a discrete structure consisting of nodes
(vertices) and the lines joining the nodes (edges) For finite graphs, V and E are finite. We can write a
graph as G = (V, E)Graph traversals— Visiting all the vertices and edges in a systematic fashion is called as a graph traversal
![Page 4: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/4.jpg)
4Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Introtuction Graphs are classified as directed and undirected
graphs
In an undirected graph, an edge is a set of two vertices where order does not make any relevance, whereas in a directed graph, an edge is an ordered pair
![Page 5: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/5.jpg)
5Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Graph Abstract Data Type Graph
create() :Graphinsert vertex(Graph,
v) :Graphdelete vertex(Graph,
v) :Graphinsert edge(Graph, u,
v) :Graphdelete edge(Graph, u,
v) :Graphis empty(Graph) :Boolean;end graph
![Page 6: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/6.jpg)
6Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Adjacency matrix (sequential representation)
Adjacency list (linked representation) Adjacency Multilist Inverse Adjacency List
Representation of Graphs
![Page 7: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/7.jpg)
7Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
The graphs represented using a sequential representation using matrices is called an adjacency matrix
Adjacency Matrix
![Page 8: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/8.jpg)
8Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Adjacency Matrix representation
![Page 9: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/9.jpg)
9Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
In an adjacency list, the n rows of the adjacency list are represented as n-linked lists, one list per vertex of the graph
We can represent G by an array Head, where Head[i] is a pointer to the adjacency list of vertex i
Each node of the list has at least two fields: vertex and link
The vertex field contains the vertex–id, and link field stores the pointer to the next node storing another vertex adjacent to i
Adjacency Lists
![Page 10: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/10.jpg)
10Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Adjacency list representation
![Page 11: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/11.jpg)
11Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Adjacency list representation
![Page 12: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/12.jpg)
12Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Adjacency Multilist Multiclass are lists where nodes
may be shared among several other lists
![Page 13: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/13.jpg)
13Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Adjacency Multilist
The node structure of such a list can be represented as follows :
![Page 14: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/14.jpg)
14Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Adjacency multilist for graph
![Page 15: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/15.jpg)
15Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Inverse Adjacency List
Inverse adjacency lists is a set of lists that contain one list for vertex
Each list contains a node per vertex adjacent to the vertex it represents
![Page 16: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/16.jpg)
16Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Graph and its inverse adjacency list
![Page 17: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/17.jpg)
17Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Depth-First Search
DFS starts at the vertex v of G as a start vertex and v is marked as visited
Then, each unvisited vertex adjacent to v is searched using the DFS recursively
Once all the vertices that can be reached from v have been visited, the search of v is complete
If some vertices remain unvisited, we select an unvisited vertex as a new start vertex and then repeat the process until all the vertices of G are marked visited
![Page 18: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/18.jpg)
18Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Depth-First Search Representation
(a) Graph and its (b) adjacency list representation
![Page 19: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/19.jpg)
19Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Spanning Tree
A tree is a connected graph with no cycles
A spanning tree is a sub-graph of G that has all vertices of G and is a tree
A minimum spanning tree of a weighted graph G is the spanning tree of G whose edges sum to minimum weight
![Page 20: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/20.jpg)
20Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Breadth-First Search
In BFS, all the unvisited vertices adjacent to i are visited after visiting the start vertex i and marking it visited
Next, the unvisited vertices adjacent to these vertices are visited and so on until the entire graph has been traversed
![Page 21: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/21.jpg)
21Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Breadth-First Search Representation
Breadth-first search sequence for the graph
![Page 22: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/22.jpg)
22Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Spanning Tree
A tree is a connected graph with no cycles
A spanning tree is a sub-graph of G that has all vertices of G and is a tree
A minimum spanning tree of a weighted graph G is the spanning tree of G whose edges sum to minimum weight
![Page 23: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/23.jpg)
23Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Spanning Tree
![Page 24: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/24.jpg)
24Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
An undirected graph is connected if there is at least one path between every pair of vertices in the graph
A connected component of a graph is a maximal
connected sub-graph, that is, every vertex in a connected component is reachable from the vertices in the component
Connected Components
![Page 25: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/25.jpg)
25Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Following fig shows the Sample graph with one connected component
![Page 26: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/26.jpg)
26Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Following fig shows the Graph with two connected components
![Page 27: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/27.jpg)
27Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Prim’s algorithm starts from one vertex and grows the rest of the tree by adding one vertex at a time, by adding the associated edges
This algorithm builds a tree by iteratively adding edges until a minimal spanning tree is obtained, that is, when all nodes are added
At each iteration, it adds to the current tree a vertex though minimum weight edge that does not complete a cycle
Prim’s Algorithm
![Page 28: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/28.jpg)
28Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Minimum Spanning Tree
Minimum Spanning Tree
![Page 29: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/29.jpg)
29Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Another way to construct a minimum spanning tree for a graph G is to start with a graph T = (V′, E′ = ø) consisting of the n vertices of G and having no edges
In Prim’s algorithm, we start with one connected component, add a vertex to have one connected component and no cycles, and end up with one connected component
Here, we start with n connected components; at each step, the number of connected components would reduce by one and end up with one connected component
Kruskal’s Algorithm
![Page 30: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/30.jpg)
30Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
The shortest path between two given vertices is the path having minimum length
This problem can be solved by one of the greedy algorithms, by Edger W. Dijkstra, often called as Dijkstra’s algorithm
Shortest Path Algorithm
![Page 31: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/31.jpg)
31Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Shortest Path Algorithm
Directed weighted graph
Shortest Path
![Page 32: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/32.jpg)
32Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Graphs are one of the most important non-linear data structures. A graph is a representation of relation
Vertices represent elements and edges represent relationships
In other words, a graph is a collection of nodes (vertices) and arcs joining pair of the nodes (edges)
Graphs are classified as directed and undirected graphs. In an undirected graph, an edge is a set of two vertices where order does not make any relevance, whereas in a directed graph, an edge is an ordered pair.
Summary
![Page 33: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/33.jpg)
33Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Graphs are implemented using an array or a linked list representation
An adjacency list is a data structure for representing a graph by keeping a list of the neighbour vertices for each vertex
An adjacency matrix is a data structure for representing a graph as a Boolean matrix where 0 means no edge and 1 corresponds to an edge
A minimum spanning tree is a tree, containing all the vertices of a graph, where the total weight of the edges is minimum
The two popularly used algorithms to compute minimum spanning tree are the following: Prim’s and Kruskal’s algorithms
Dijkstra’s algorithm is another common algorithm for graphs to find the shortest path between two vertices of a graph
Summary
![Page 34: 8. Graph - Data Structures using C++ by Varsha Patil](https://reader035.vdocuments.us/reader035/viewer/2022062823/5876d4b11a28ab1d238b5497/html5/thumbnails/34.jpg)
34Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
End of
Chapter 8…!