Unit 5 (Edited)
Unit 5 (Edited)
Graph Terminologies:
Eg:
Here 4 nodes i.e. 1, 2, 3 and 4 are called vertices and normally denoted as:
V = {1,2,3,4}
Edge: If u and v are vertices, then an arc or a line joining 2 vertices u and v is called an edge.
Eg 1:
There is no direction for the edge between vertex 1 and vertex 6 and hence it is called
undirected edge.
In general, if e= (u, v) then the nodes u and v are called end points of directed edge.
In this graph edge (1, 6) is same as edge (6,1) since there is no direction associated with that
edge. So, (u, v) and (v, u) represent same edge.
Eg 2:
There is a direction for the edge originating at vertex 1 (called tail of the edge) and heading
towards vertex 6 (called head of edge) and hence it is called directed edge.
In general, if a directed edge is represented by directed pair <u, v>, u is called the tail of the
edge and v is the head of the edge. So directed pair <u, v> is different from the directed pair
<v, u>. So <u, v> and <v, u> represent 2 different edges.
A finite set of vertices also called as nodes. A finite set of ordered pair of the form (u, v)
called as edge.
(or)
A graph G= (V, E) is a collection of two sets V and E, where V= Finite number of vertices
Eg:
E= {<1,6>, <1,2>, <2,3>, <4,3>, <5, 3>, <5, 6>, <6, 4>} is set of directed edges
|E| = |{<1,6>, <1, 2>, <2,3>, <4,3>, <5,3>, <5,6>, <6,4> }| =7 represent the number of edges
in the graph
A graph G = (V, E) in which every edge is called a directed graph. The directed graph is also
called digraph. A graph G = (V, E) in which every edge is undirected is called an undirected
graph.
What is a self – loop (self-edge)?
A loop is an edge which starts and end on the same vertex. A loop is represented by an
ordered pair (i, i). This indicates that the edge originates and ends in the same vertex. A loop
is called self-edge or self-loop.
What is multigraph?
A graph with multiple occurrence of the same edge between any 2 vertices is called
multigraph.
Here, there are 2 edges between the nodes 1 and 4 and there are 3 edges between the nodes 4
and 3.
A graph G= (V, E) is said to be a complete graph, if there exists an edge between every pair
of vertices.
The graph (a) below is complete. So in complete graph of n vertices, there will be n(n-1)/2
edges. Even if one edge is removed as in graph (b) then it is not a complete graph.
Walk: A walk is an alternating sequence of vertices and connecting edges. A walk can end
on the same vertex on which it began or on a different vertex. A walk can travel over any
edge and any vertex any number of times.
What is a path?
Definition: Let G= (V, E) be a graph. A path from vertex u to vertex v in an undirected graph
is a sequence of adjacent vertices (u, v0, v1, v2, …...vk, v) such that: (u, v0), (v0, v1) ….(vk, v)
are the edges in G.
Consider the graph:
In the graph, the path from vertex 1 to 4 is denoted by 1, 2, 3, 4 which can also be written as
(1,2), (2,3), (3,4)
Definition: Let G= (V, E) be a graph. A path from vertex u to vertex v in an undirected graph
is a sequence of adjacent vertices <u, v0, v1, v2, …...vk, v> such that: <u, v0>, <v0, v1> ….<vk,
v> are the edges in G.
In the graph, the path from vertex 1 to 3 is denoted by 1, 4, 2, 3 which can also be written as
<1,4>, <4, 2>, <2, 3>
A simple path is a path in which all vertices except possibly the first and last are distinct.
Eg 2: In the graph, the path 1, 2, 3, 2 is not a simple path since the nodes in sequence are not
distinct. The node 2 appears twice in the path.
Eg 1: In the graph, the path 1, 4, 2, 3 is simple path since each node in the sequence is
distinct.
Eg 2: The sequence 1, 4, 3 is not a path since there is no edge <4, 3> in the graph.
Eg 1:
In the above undirected graph, the path (1, 2, 3, 4) has length 3 since there are 3 edges
(1, 2), (2, 3), (3, 4). The path 1, 2, 3 has length 2 since there are two edges (1, 2), (2, 3)
Eg 2:
In the above directed graph, the path <1, 2, 3, 4> has length 3 since there are 3 edges <1,2>,
<2,3>, <3,4>. The path <1, 4, 2> has length 2 since there are 2 edges <1, 4>, <4, 2>
Trail: A trail is a walk that does not pass over the same edge twice. A trail might visit the
same vertex twice, but only if it comes and goes from a different edge each time.
Cycle (circuit):
A cycle is a path in which the first and last vertices are same.
Eg: The path <4, 2, 3, 4> shown in directed graph is a cycle, since the first node and last node
are same. It can also be represented as <4, 2>, <2, 3>, <3, 4> <4, 2>
Note: A graph with atleast one cycle is called cyclic graph and a graph with no cycles is
called acyclic graph. A tree is an acyclic graph and hence it has no cycle.
Connected Graph:
In an undirected graph G, two vertices u and v are said to be connected if there exists a path
from u to v. Since G is undirected graph, there exists a path from v to u also. A graph G
(directed or undirected) is said to be connected if and only if there exist a path between every
pair of vertices.
Disconnected graph:
Let G = (V, E) be a graph. If there exist atleast one vertex in a graph that cannot be reached
from other vertices in the graph, then such a graph is called disconnected graph.
Euler Graphs: A connected graph G is called a Euler graph, if there is a closed trail which
includes every edge of the graph G.
a) Euler path: An Euler path is a path that uses every edge of a graph exactly once. A Euler
path starts and ends at different vertices.
b) Euler circuit: An Euler circuit is a circuit that uses every edge of a graph exactly once. A
Euler circuit always starts and ends at the same vertex. A connected graph G is a Euler graph
if and only if all vertices of G are of even degree, and a connected graph G is Eulerian if and
only if its edge set can be decomposed into cycles.
The above graph is a Euler graph as a 1 b 2 c 3 d 4 e 5 c 6 f 7 a covers all the edges of the
graph.
Non-Euler Graph:
Here degree of vertex b and d is 3, an odd degree and violating the Euler graph condition.
Hamiltonian Circuits: A Hamiltonian circuit is a circuit that visits every vertex once with no
repeats. Being a circuit, it must start and end at the same vertex.
Hamiltonian Path: A Hamiltonian path also visits every vertex once with no repeats, but
does not have to start and end at the same vertex.
Solution: No the above graph does not have a Hamiltonian circuit as there are two vertices
with degree one in the graph.
Directed Graph:
In representing of graph there is a directions are shown on the edges then that graph is called
Directed graph.
That is, a graph G= (V, E) is a directed graph, Edge is a pair (v, w), where v, w ∈ V, and the
pair is ordered. Means vertex ‘w’ is adjacent to v. Directed graph is also called digraph.
Undirected Graph:
In graph vertices are not ordered is called undirected graph. Means in which (graph) there is
no direction (arrow head) on any line (edge).
A graph G= (V, E) is a directed graph, Edge is a pair (v, w), where v, w ∈ V, and the pair is
not ordered. Means vertex ‘w’ is adjacent to ‘v’, and vertex ‘v’ is adjacent to ‘w’
Representation of Graphs
Graphs are used to represent many real life applications: Graphs are used to represent
networks. The networks may include paths in a city or telephone network or circuit network.
Graphs are also used in social networks like LinkedIn, Facebook. For example, in Facebook,
each person is represented with a vertex (or node). Each node is a structure and contains
information like person id, name, gender and locale.
Following two are the most commonly used methods for representations of graph:
1) Adjacency Matrix
2) Adjacency List
1) Adjacency Matrix: Let G = (V, E) be a graph where V is set of vertices and E is set of
edges. Let N be the number of vertices in graph G.
Pros: Representation is easier to implement and follow. Removing an edge takes O (1) time.
Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient and can be
done O (1).
Cons: Consumes more space O(V^2). Even if the graph is sparse (contains less number of
edges), it consumes the same space. Adding a vertex is O(V^2) time.
2) Adjacency List:
Let G =(V, E) be a graph. An adjacency linked list is an array of n linked list where n is the
number of vertices in graph G.
Each location of the array represents a vertex of the graph. For each vertex u ∈ V, a linked
list consisting of all the vertices adjacent to u is created and stored in A[u]. The resulting
array A is an adjacency list.
Note: From the definition we get to know that if i, j and k are the vertices adjacent to the
vertex u, then i, j and k are stored in a linked list and starting address of linked list is stored in
A[u] as:
Eg: Directed and undirected graph along with adjacency linked list:
Definition: A graph in which a number is assigned to each edge in a graph is called weighted
graph. These numbers are called costs or weights. The weights are represented depending in
the problem.
Eg 1: In the following graph the values 10, 20, 30 and 40 are the weights associated with 4
edges <1, 3>, <1, 2>, <3, 4> and <2, 4>
Eg 2:
The weighted graph can be represented using adjacency matrix as well as adjacency linked
list. The adjacency matrix consisting of cost (weight) is called cost adjacency matrix. The
adjacency linked list consisting of cost(weight) is called cost adjacency linked list.
Let G= (V, E) be the graph where V is set of vertices and E is set of edges with n number of
vertices. The cost adjacency matrix A of a graph G is formally defined as:
What is cost adjacency linked list?
C function to read adjacency matrix:
int i, j;
scanf(“%d”,&a[i][j]);
}
int i, j, , item;
scanf(“%d”,&m);
if(m==0)
continue;
scanf(“%d”,&item);
a[i]=insert_rear(item, a[i]);
Graph traversal:
1) BFS: Breadth First Search (BFS) is a method of traversing the graph from an arbitrary vertex
say u. First visit the node u. Then we visit all the neighbours of u and so on. That is first we
have to visit all the neighbouring nodes first before moving to the next level neighbours. The
search will terminate when all the vertices have been visited.
BFS can be implemented using queue. As we visit node, it is inserted into queue. Now we
have to delete a node from the queue and see the adjacent nodes which have not been visited.
The unvisited nodes are inserted into queue and marked as visited.
Eg 1: Traverse the following graph by BFS and print all the vertices reachable from start
vertex S.
Eg 2: Traverse the following graph by BFS and print all the vertices reachable from start
vertex a. Resolve ties by the vertex alphabetical order.
Step 1:
Step 2:
BFS traversal uses queue data structure which requires insert rear and delete front function.
NODE temp;
if(first==NULL)
}
2) DFS: Depth First Search (DFS) is a method of traversing the graph by visiting each node
of the graph in systematic order. As the name implies depth first search means “to search
deeper in the graph”.
Definition: In DFS, a vertex u is picked as source vertex and is visited. The vertex u at this
point is said to be unexplored. The exploration of vertex u is postponed and a vertex v is
adjacent to u is picked and is visited. So here search begins at the vertex v. There may be still
some nodes which are adjacent to u but not visited. When vertex v is completely examined,
then only u is examined. The search will terminate when all the vertices have been examined.
Hence the search continues deeper and deeper in the graph until all the vertices are visited.
Step 1: Select node u as the start vertex (select in alphabetical order), push u onto stack and
mark it as visited. We add u to S for marking.
For vertex u on top of the stack, find the next immediate adjacent vertex.
if v is adjacent
else
else
end if
end while
Step 3: Repeat step 1 and 2 until all the vertices in the graph.
Eg 1:
Eg 2: DFS
Solution:
Output: ABEFCGDH
Eg 3: Consider the graph G along with its adjacency list, given in the figure below. Calculate
the order to print all the nodes of the graph starting from node H, by using depth first search
(DFS) algorithm.
Solution: H A B C E F G D
Eg 3: Traverse the following graph using DFS and display the nodes reachable from a given
source vertex a.
Solution: Since a vertex a is the last in alphabetical order, it is selected as the start vertex.
Same procedure should be followed like in BFS but there are 2 changes here:
b) In BFS, all the nodes adjacent and which are not visited are considered. In DFS, only one
adjacent which is not visited earlier should be considered. Rest of the procedure remains
same.
Operations on graph:
If the edge between 1 and 4 has to be removed, then the above graph and the adjacency list
transforms to:
Approach: The idea is to represent the graph as an array of vectors such that every vector
represents adjacency list of the vertex.
Adding an edge: Adding an edge is done by inserting both of the vertices connected by
that edge in each other’s list. For example, if an edge between (u, v) has to be added,
then u is stored in v’s vector list and v is stored in u’s vector list. (push_back)
Deleting an edge: To delete edge between (u, v), u’s adjacency list is traversed
until v is found and it is removed from it. The same operation is performed for v.(erase)
Example:
Approach:
Delete the vertex from the indexes map (This map is used for DFS or BFS traversals)
since there will be no traversal from the deleted vertex.
Remove the vertex from the first map (vertices are stored as a key in the map). This
will delete vertex and all outgoing edges from the deleted vertex.
To delete incoming edges towards deleted vertex from the other vertices, traverse all
the linked list for other vertices and delete the vertex if there is any. This will delete
the edge from other vertices to the deleted vertex.