Unit Iv
Unit Iv
E-CSE
DEPARTMENT OF CSE
DEFINITION
A Graph G = (V,E) consists of a set of vertices(V) and set of edges (E). Each
edge is a pair (v,w), where v,w € V. Edges are sometimes referred to as
arcs.
Example:
V = { V1,V2,V3,V4}
E = {E1, E2, E3, E4 }
REPRESENTATION OF GRAPHS
The two commonly used representations of Graphs are:
1. Adjacency Matrix
2. Adjacency List
1. Adjacency Matrix
Consider a graph C of n vertices and the matrix M. If there is an edge
present between vertices Vi and Vj, then M[i][j] = 1 else M[i][j] = 0. For an
undirected graph, M[i][j] = M[i][j = 1.
An Adjacency Matrix Representation for Undirected Graph
1. Adjacency List
The type in which a graph is created with the linked list is called adjacency
list. So all the advantages of linked list can be obtained in this type of graph.
We need not have a prior knowledge of maximum number of nodes.
The linked list representation of the head nodes and the adjacent nodes are
given below.
Struct head
{
char data;
struct head *down;
struct head *next;
}
struct node
{
int ver;
struct node *link
}
TYPES OF GRAPHS
Graphs are classified into two types. They are:
i. Directed Graph.
ii. Undirected Graph.
i. Directed Graph: The directions are shown on the edges. Consider the
graph given below:
V1
E1
E2
V2 V3
E3 E4
V4
In the above graph, the edge E1 is in between the vertices V1 and V2. The
Vertex V1 is called the head and V2 is called the tail. It is also referred as
digraph.
ii. Undirected Graph: In this type of graph, the edges are not directed.
Graph Terminologies:
Sub graph: A Sub graph G’ of graph G is a graph such that the set of
vertices and
set of edges of G’ are proper subset of the set of edges of G.
Length of a path: A path is a sequence of vertices w1, w2, w3, ..., wN, such
that (wi, wi+1) E for 1 < i < N. The length of a path is the number of edges
on the path, which is N-1. A simple path is one such that all vertices are
distinct, except that the first and the last could be the same.
Cycle: A cycle in a directed graph is a path of length at least 1 such that w1
= wN.
The indegree of a is 0.
The outdegree of a is 2.
Graph Traversal:
ALGORITHM
1. Create a graph.
2. Read the vertex from which you want to traverse the graph say Vi.
3. Initialise the visited array to 1 at the index of Vi
In BFS the queue is maintained for storing the adjacent nodes and an array
“visited” is maintained for keeping the track of visited nodes. i.e. once a
particular t is visited it should not be revisited again.
Increment front, delete the node from Queue and print it.
ALGORITHM
Step 1: Choose any node in the graph. Designate it as the search node and
mark it as visited.
Step 2: Using the adjacency matrix of the graph, find a node adjacent to the
search node that has not been visited yet. Designate this as the new search
node and mark it as visited.
Step 3: Repeat Step 2 using the new search node. If no node satisfying Step
2 can be found, return to the previous search node and continue.
Step 4: When a return to the previous node in step 3 is impossible, the
search from the originally chosen search node is complete.
Step 5: If the graph still contains unvisited nodes, choose any node that has
not been visited and repeat Step 1 through Step 4.
Example:
In DFS the basic data structure for storing the adjacent vertices is stack.
Step 1: Start with vertex 1, print it so ‘1’ gets printed. Mark I as visited.
Void DFS(Vertex V)
{
visisted[V]=True;
for each W adjacent to V
if(!visisted[W])
DFS(W);
}
TOPOLOGICAL SORT
DEFINITION
Topological ordering is not possible, if the graph has a cycle, since for two
vertices v and w in a cycle, v precedes w and w precedes v.
Step 1
Number of l’s present in each column of adjacency matrix represents the
indegree of corresponding vertex. In the given graph, indegree of a = 0,
b=1, c=2 and d=2.
Step 2
Enqueue the vertex, whose indegree is ‘0’
Since vertex ‘a’ is 0, so place it on the queue.
Step 3
Dequeue the vertex ‘a’ from the queue and decrement the indegree’s of its
adjacent vertex’
Step 4
Dequeue the vertex ‘b’ from Q and decrement the indegree’s of its adjacent
vertex.
Step 5
Dequeue the vertex c’ from Q and decrement the indegree’s of its adjacent
vertex..
Step 6
Dequeue the vertex ‘d’.
The topological sort for the graph is the vertices which are dequeued is
shown below.
The topological ordering is V1, V2, V5, V4, V3, V7, V6.
EULER CIRCUITS
Definition:
An Eulerian path is a path in a graph which visits each edge exactly once.
Similarly, an Eulerian circuit is an Eulerian path which starts and ends on
the same vertex. They were first discussed by Leonhard Euler while solving
the famous Seven Bridges of Königsberg problem in 1736.
Given the graph on the right, is it possible to construct a path (or a cycle, i.e.
a path starting and ending on the same vertex) which visits each edge exactly
once?
Graphs which allow the construction of so called Eulerian circuits are called
Eulerian graphs. Euler observed that a necessary condition for the existence
of Eulerian circuits is that all vertices in the graph have an even degree, and
that for an Eulerian path either all, or all but two (i.e., the two endpoint)
vertices have an even degree.
Sometimes a graph that has an Eulerian path, but not an Eulerian circuit (in
other words, it is an open path, and does not start and end at the same vertex)
is called semi-Eulerian.
Graph 1 Graph 2
Graph 3 Graph 4
Graph 5 Graph 6
What is the relationship between the nature of the vertices and the kind of
path/circuit that the graph contains?
1 0 10 C
2 0 6 C
3 2 6 P
4 2 4 P
5 4 1 N
6 8 0 N
2. A graph with 2 odd vertices and some even vertices contains an Euler
path.
3. A graph with more than 2 odd vertices does not contain any Euler
path or circuit.
Applications of Graph:
Graphs and Graph theory find various application in many real life
applications. Some of them are listed below:
3. Networks have many uses in the practical side of graph theory, network
analysis (for example, to model and analyze traffic networks). Within
network analysis, the definition of the term "network" varies, and may often
refer to a simple graph.
ii. Secondly, analysis to find a measurable quantity within the network, for
example, for a transportation network, the level of vehicular flow within any
portion of it.