Module-5 Graph
Module-5 Graph
G= (V,E)
V(G3) = Find ?
E(G3) = Find ?
Graph Terminology
1.Vertex : An individual data element of a graph is called as
Vertex. Vertex is also known as node. In above example graph,
A, B, C, D & E are known as vertices.
2.Edge : An edge is a connecting link between two vertices. Edge is
also known as Arc. An edge is represented as (starting Vertex,
ending Vertex).
In above graph, the link between vertices A
three types:
Edge
An edge is a connecting link between two vertices. Edge is also known as Arc. An
edge is represented as (starting Vertex, ending Vertex). For example, in above
graph, the link between vertices A and B is represented as (A,B).
Directed Graph
A graph with only directed edges is said to be directed graph.
Mixed Graph
A graph with undirected and directed edges is said to be mixed graph.
Origin
If an edge is directed, its first endpoint is said to be origin of it.
Destination
If an edge is directed, its first endpoint is said to be origin of it and the other
endpoint is said to be the destination of the edge.
Graph Terminology
Adjacent
If there is an edge between vertices A and B then both A and B are said to
be adjacent. In other words, Two vertices A and B are said to be adjacent if
there is an edge whose end vertices are A and B.
Incident
An edge is said to be incident on a vertex if the vertex is one of the
endpoints of that edge.
Outgoing Edge
A directed edge is said to be outgoing edge on its origin vertex.
Incoming Edge
A directed edge is said to be incoming edge on its destination vertex.
Degree
Total number of edges connected to a vertex is said to be degree of that
vertex.
Indegree
Total number of incoming edges connected to a vertex is said to be indegree
of that vertex.
Outdegree
Total number of outgoing edges connected to a vertex is said to be outdegree
of that vertex.
Self-loop
An edge (undirected or directed) is a self-loop if its two endpoints coincide.
Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges.
Path
A path is a sequence of alternating vertices and edges that starts at a vertex
and ends at a vertex such that each edge is incident to its predecessor and
successor vertex.
11/28/2021 11
3.Weighted Edge - A weighted edge is an edge with cost on it.
Types of Graphs 1.Undirected Graph
A graph with only undirected edges is said to be undirected graph.
2.Directed Graph
A graph with only directed edges is said to be directed graph.
3.Complete Graph
A graph in which any V node is adjacent to all other nodes present in
the graph is known as a complete graph. An undirected graph contains
the edges that are equal to edges = n(n-1)/2 where n is the number of
vertices present in the graph. The following figure shows a complete
graph.
4.Regular Graph
Regular graph is the graph in which nodes are adjacent to each other, i.e., each
node is accessible from any other node.
5.Cycle Graph
A graph having cycle is called cycle graph. In this case the first and last nodes are
the same. A closed simple path is a cycle.
Sub Graph
A graph S is said to be a sub graph of a graph G if all the vertices and all the
edges of S are in G, and each edge of S has the same end vertices in S as in
G. A subgraph of G is a graph G’ such that V(G’) V(G) and E(G’) E(G)
In the above graph,the indegree of vertices v1, v3 is 2, indegree of vertices v2, v5 is 1 and indegree of v4 is
zero.
Connected Graph
A graph G is said to be connected if there is at least one path between
every pair of vertices in G. Otherwise,G is disconnected.
In the above graph,the indegree of vertices v1, v3 is 2, indegree of vertices v2, v5 is 1 and indegree of v4 is
zero.
A connected graph G A disconnected graph G
This graph is disconnected because the vertex v1 is not connected with the
other vertices of the graph.
Degree
In an undirected graph, the number of edges connected to a node is called the
degree of that node or the degree of a node is the number of edges incident on
it.
In the above graph, degree of vertex v1 is 1, degree of vertex v2 is 3, degree
of v3 and v4 is 2 in a connected graph.
Indegree
The indegree of a node is the number of edges connecting to that node or in
other words edges incident to it.
In the graph , the indegree of vertices v1, v3 is 2, indegree of vertices v2, v5 is 1
and indegree of v4 is zero.
11/28/2021 17
Graph Representations
Graph data structure is represented using following representations...
Adjacency Matrix
Incidence Matrix
Adjacency List
Adjacency Matrix
In this representation, graph can be represented using a matrix of size total
number of vertices by total number of vertices. That means if a graph with 4
vertices can be represented using a matrix of 4X4 class.
In this matrix, rows and columns both represents vertices. This matrix is
filled with either 1 or 0. Here, 1 represents there is an edge from row vertex
to column vertex and 0 represents there is no edge from row vertex to
column vertex.
For example, consider the following undirected graph representation...
Array based data structure for
Undirected Graph graph
Adjacency Matrix
1 2 3 4 5 6
1 0 1 0 0 1 0
2 1 0 1 0 1 0
3 0 1 0 1 0 0
4 0 0 1 0 1 1
V= {1,2,3,4,5,6}
E= {(1,2), (1,5), 5 1 1 0 1 0 0
(2,5), (2,3),
6 0 0 0 1 0 0
(3,4),
(4,5), (4,6)}
Size = O(𝑛2)
1
9
11/28/2021 20
Array based data structure for
graph
Advantage of Adjacency Matrix :
• Determining whether there is an edge from x to y in O(1) time
for any two vertices x and y.
2
1
Directed graph representation...
Incidence Matrix
In this representation, graph can be represented using a matrix of size total number
of vertices by total number of edges. That means if a graph with 4 vertices and 6
edges can be represented using a matrix of 4X6 class.
In this matrix, rows represents vertices and columns represents edges. This matrix
is filled with either 0 or 1 or -1. Here, 0 represents row edge is not connected to
column vertex, 1 represents row edge is connected as outgoing edge to column
vertex and -1 represents row edge is connected as incoming edge to column vertex.
1 2 5
2 1 3 5
3 2 4
4 3 5 6
V= {1,2,3,4,5,6}
E= {(1,2), (1,5), 5 1 2 4
(2,5), (2,3),
6 4
(3,4),
(4,5), (4,6)}
Size = O(𝑛 + 𝑚)
2
4
Which data structure is commonly used for
storing graphs ?
• Reasons: Adjacency lists
2
• But, there are a few 5
3.Adjacency Multilists
In the adjacency-list representation of an undirected graph each edge (u, v) is
represented by two entries one on the list for u and the other on tht list for v. As we
shall see in some situations it is necessary to be able to determin ie ~ nd enty for a
particular edge and mark that edg as having been examined. This can be
accomplished easily if the adjacency lists are actually maintained as multilists (i.e.,
lists in which nodes may be shared among several lists). For each edge there will be
exactly one node but this node will be in two lists (i.e. the adjacency lists for each
of the two nodes to which it is incident).
For adjacency multilists, node
structure is typedef struct edge
*edge_pointer; typedef struct edge
{
short int marked; int
vertex1, vertex2;
edge_pointer path1, path2;
};
edge_pointer graph[MAX_VERTICES];
11/28/2021 26
Lists: vertex 0: N0->N1->N2, vertex 1: N0->N3->N4 vertex 2: N1->N3-
>N5, vertex 3: N2->N4->N5
11/28/2021 27
Figure: Adjacency multilists for given graph
4. Weighted edges
In many applications the edges of a graph have weights assigned to them. These
weights may represent the distance from one vertex to another or the cost of going
from one; vertex to an adjacent vertex In these applications the adjacency matrix
entries A [i][j] would keep this information too. When adjacency lists are used the
weight information may be kept in the list’nodes by including an additional field
weight. A graph with weighted edges is called a network.
11/28/2021 28
Graph traversal
Definition:
A vertex y is said to be reachable from x if there is a path from x to y.
11/28/2021 30
Traversal a graph
• Depth first traversal
• Breadth first traversal
11/28/2021 31
ELEMENTARY GRAPH OPERATIONS
Given a graph G = (V E) and a vertex v in V(G) we wish to visit all vertices in G
that are reachable from v (i.e., all vertices that are connected to v). We shall look at
two ways of doing this: depth-first search and breadth-first search. Although these
methods work on both directed and undirected graphs the following discussion
assumes that the graphs are undirected.
Depth-First Search
pop one vertex from the stack. Step 6: Repeat steps 3, 4 and 5 until stack
becomes Empty.
Step 7: When stack becomes Empty, then produce final spanning tree by removing
unused edges from the graph This function is best described recursively as in
Program.
11/28/2021 33
This function is best described recursively as in Program.
#define FALSE 0
#define TRUE 1
int visited[MAX_VERTICES];
void dfs(int v)
{
node_pointer w;
visited[v]= TRUE;
printf(“%d”, v);
for (w=graph[v]; w; w=w-
>link) if (!visited[w->vertex])
dfs(w->vertex);
}
Consider the graph G of Figure 6.16(a), which is represented by its adjacency lists as
in Figure 6.16(b). If a depth- first search is initiated from vertex 0 then the vertices of
G are visited in the following order: 0 1 3 7 4 5 2 6. Since DFS(O) visits all vertices
that can be reached from 0 the vertices visited, together with all edges in G
incident to these vertices form a connected component of G.
11/28/2021 34
Figure: Graph and its adjacency list representation, DFS spanning tree
Analysis or DFS:
When G is represented by its adjacency lists, the vertices w adjacent to v can be
determined by following a chain of links. Since DFS examines each node in the
adjacency lists at most once and there are 2e list nodes the time to complete the
search is O(e). If G is represented by its adjacency matrix then the time to
determine all vertices
2
adjacent to v is O(n). Since at most n vertices are visited
the total time is O(n ).
11/28/2021 35
11/28/2021 36
11/28/2021 37
Breadth-First Search
In a breadth-first search, we begin by visiting the start vertex v. Next all
unvisited vertices adjacent to v are visited. Unvisited vertices adjacent to
these newly visited vertices are then visited and so on. Algorithm BFS
struct queue *queue_pointer; typedef struct queue {
int vertex;
11/28/2021 42
int main()
{ int v,i,j;
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=0;i<n;i++) // mark all the vertices as not visited
{
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for (i=0;i<n;i++)
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
f=r=0;
q[r]=v;
printf("\n BFS traversal is:\n");
visited[v]=1; // mark the starting vertex as visited
printf("%d ",v);
bfs(v);
if(r != n-1)
printf("\n BFS is not possible");
}
11/28/2021 43
11/28/2021 44
• Construct adjacency matrix & adjacency list for
the graph
11/28/2021 45
11/28/2021 46
Topological Sorting:
11/28/2021 47
11/28/2021 48
11/28/2021 49
Directed Acyclic Graph
11/28/2021 50
Algorithm
11/28/2021 51
Example
Applications
11/28/2021 60
11/28/2021 61
11/28/2021 62
11/28/2021 63