Graph
Graph
kashiram pokharel
kashiram pokharel
Introduction
kashiram pokharel
Uses of graph
kashiram pokharel
Not connected graph:
• A graph in which at least one node is not connected or all nodes may
not be connected is called not-connected graph.
kashiram pokharel
Undirected graph:
kashiram pokharel
Simple graph:
kashiram pokharel
Multi graph
kashiram pokharel
Pseudo graph:
kashiram pokharel
Strongly connected graph
• Connectivity in an undirected graph means that every vertex can reach
every other vertex via any path. If the graph is not connected the graph can
be broken down into Connected Components.
• Strong Connectivity applies only to directed graphs. A directed graph is
strongly connected if there is a directed path from any vertex to every
other vertex. This is same as connectivity in an undirected graph, the only
difference being strong connectivity applies to directed graphs and there
should be directed paths instead of just paths.
• Similar to connected components,
a directed graph can be broken down
into Strongly Connected Components.
kashiram pokharel
Weakly connected graph
• A digraph is strongly connected if every vertex is reachable from every
other following the directions of the arcs. I.e., for every pair of
distinct vertices uu and vv there exists a directed path from uu to vv.
kashiram pokharel
Weighted graph:
kashiram pokharel
Unweighted graph:
kashiram pokharel
Complete graph (Kn ):
• A simple graph is said to be complete if every vertex in G is connected with
every other vertex .In other words, we can say a complete graph contains
exactly one edge between each pair of distinct vertices.
𝑛(𝑛−1)
• A complete graph is denoted by Kn . Kn has exactly edges
2
kashiram pokharel
Bipartite graph :
• A graph G is said to be bi-partite graph if it’s vertices are divided into
two subset M and N .such that
• vertices of M subset are connected to the vertices of N subset, but
• the vertices of same parts are never connected.
• If the vertices of first part are connected to all the vertices of another
subset (second part) then Bipartite graph is called complete bi-partite
graph and is denoted by Km,n where m is the number of vertices in M
subset and n is the number of vertices in N subset.
• For standardization, we will assume m<=n
kashiram pokharel
• The graph Km,n has m*n edge
• How many edge have each of the following
graph?
• K3,3
• Since , K3,3 is complete bipartite graph
therefore, the number of edge =3*3=9
kashiram pokharel
Properties of bi-partite graph:
kashiram pokharel
Euler theorem for planner graph
• Let V be the number of vertices and E be the number of edges and f
be the number of faces .Then Euler formula is defined as:
V-E+F=2
kashiram pokharel
Corollary 1:
kashiram pokharel
Corollary 3:
• If a connected planar simple graph has e edges and v vertices with v
3 and no circuits of length 3, then e 2v - 4.
• Is K3,3 planar?
K3,3 has 6 vertices and 9 edges.
Obviously, v 3 and there are no circuits of length 3.
If K3,3 were planar, then e 2v – 4 would have to be true.
e 2v – 4
9 = 2*6 – 4
9 8 .which is false because e must be 8.
But e = 9.
So K3,3 is nonplanar
kashiram pokharel
Cycle graph(Cn)
• In graph theory, a cycle graph or circular graph is a graph that consists of a
single cycle, or in other words, some number of vertices connected in a
closed chain.
• The cycle graph with n vertices is called Cn. The number of vertices
in Cn equals the number of edges, and every vertex has degree 2; that is,
every vertex has exactly two edges incident with it.
kashiram pokharel
Wheel graph (Wn)
• A wheel graph is a graph formed by connecting a single vertex to all
vertices of a cycle. A wheel graph with n vertices can also be defined
as the 1-skeleton of an (n-1)-gonal pyramid.
• A wheel graph is obtained from a cycle graph Cn-1 by adding a new
vertex. That new vertex is called a Hub which is connected to all the
vertices of Cn.
• No. of edges in Wn = No. of edges from hub to all other vertices +
No. of edges from all other nodes in cycle graph without a hub.
= (n–1) + (n–1)
= 2(n–1)
kashiram pokharel
Degree of a graph
• In an undirected graph, the number of edges connected to a node is
called the degree of that node. i.e. degree of a node is the number of
edges incident on it.
• The total number of edges incident on a particular vertex is called it’s
degree. let v be the vertex of a graph ,then it’s degree is denoted by
deg(v).
• In case of directed graph, degree is the sum of in-degree and out-
degree.
• A loop at a vertex contributes twice to the degree of that vertex
kashiram pokharel
c d
b
a g f e
kashiram pokharel
In case of Directed Graph:
• In-degree:
• The in-degree of a node is the number of edges incoming to that node or The
number of vertices adjacent to v.
• Denoted by deg(v).
• Out-degree:
• The out-degree of a node is the number of edges going outside from the
node. Or The number of vertices adjacent from v
• Denoted by deg+(v)
• Note:
• A loop at a vertex contributes 1 to both the in-degree and out-degree.
kashiram pokharel
Theorem 1: (handshaking theorem)
kashiram pokharel
• deg(a)= deg(f)= deg(e)= deg(d)=2
• deg(b)= deg(c)=3
• L.H.S
• 2e=2*7=14
• R.H.S.
degv
vV
• =2+2+2+2+3+3
• =14
• Therefore,
• LHS=RHS
kashiram pokharel
Regular graph:
• A graph G is called regular graph if it’s all vertices are of the same
degree or valency. i.e. a regular graph is a graph where each vertex
has the same number of neighbors
• A regular directed graph must also satisfy the stronger condition that
the indegree and outdegree of each vertex are equal to each other.
• If the degree of each vertex is k then graph is called k-regular graph or
regular graph of degree k.
• All complete graph are regular graph but all regular graph are not
complete graph.
kashiram pokharel
Some terms about the regular graph:
• All the vertices have the same degree k.
• Every null graph is a regular graph of degree zero.
• A regular graph of degree 1 has one line
• If G is a two –regular graph. Then graph has a cycle.
• If G is a graph of degree 3 ,it is called a cubic graph.
kashiram pokharel
Shortest path algorithm:
• This algorithm is used to solve the single source shortest path problem on a
weighted directed graph
• All the edge weights must be non-negative.
• compute the length of shortest path from a source to all other remaining
vertices.
• each node is labeled with dist predecessor and status.
• dist of node represents the shortest dist of that node from the source
node and
• predecessor of a node represents the node which precedes the given node
in shortest path from source.
• Status of a node can be permanently visited or not.
kashiram pokharel
Procedure:
• Initially make source node as the current working node. All other
node are made unvisited node.
• Label a source node with 0 and all others with
• Examine all the temporary neighbors of the current working nodes
and after checking the condition for minimum weight re-label the
required node.
• From all the temporary nodes find out the node which has minimum
value of dist , make the node permanent and now this is the current
working node.
• Repeat step 2 and 3 until destination node is made permanent.
kashiram pokharel
• set it to zero for our initial node and to infinity for all other nodes.
• Set the initial node as current. Mark all other nodes unvisited. Create a set of all the
unvisited nodes called the unvisited set.
• For the current node, consider all of its unvisited neighbors and calculate
their tentative dist from current node and If that node was previously marked with a
dist greater than current dist then relabel it with current value. Otherwise, keep the
previous value.
To update labels:
Lk(a, v) = min{Lk-1(a, v), Lk-1(a, u) + w(u, v)}
• When we are done considering all of the neighbors of the current node, mark the
current node as visited and remove it from the unvisited set. A visited node will never
be checked again.
• When unvisited set is empty then algorithm is stop and The algorithm has finished.
• Otherwise, select the unvisited node that is marked with the smallest tentative dist, set
it as the new "current node", and gokashiram
back to step 3.
pokharel
Find the shortest path using dijkstra algorithm:
• Let source node =A
• Destination node=F
kashiram pokharel
• Label 0 for starting vertex and other ∞
kashiram pokharel
• Check adjacent node of A and mark node A as visited node
L(A,B)=min{dist(B) ,{dist(A)+dist(A,B)}} min(∞ ,0+2) relabel B by 2
L(A,C) =min{dist(C),{ dist(A)+dist(A,C)} min(∞ ,0+6) relabel C by 6
kashiram pokharel
• Here B is in shortest dist then, Check adjacent node of B and mark node B
as visited node
• L(A,D)=min{dist(D) ,{dist(B)+dist(B,D)}} min(∞ ,2+7) relabel D by 9
L(A,C) =min{dist(C),{ dist(B)+dist(B,C)} min(6 ,2+3) relabel C by 5
kashiram pokharel
• Now check adjacent node of c, and mark node C as visited node
• L(A,E)=min{dist(E), (dist(C)+dist(C,E))} min(∞ ,5+9) relabel E by 14
• L(A,D)=min{dist(D), (dist(C)+dist(C,D))}min(9 ,5+2) relabel D by 7
kashiram pokharel
• Check adjacent node of D, and mark node D as visited node
• L(A,E)=min{dist(E), (dist(D)+dist(D,E))} min(14,7+6 ) relabel E by 13
• L(A,F)=min{dist(D), (dist(D)+dist(D,F))}min(∞ ,7+10 ) relabel D by 17
kashiram pokharel
• check adjacent node of E, and mark node E as visited node
• L(A,F)=min{dist(F), (dist(E)+dist(E,F))} min(17,13+5 ) relabel F by 17
• Hence final graph becomes.
kashiram pokharel
kashiram pokharel
kashiram pokharel
Transitive closure
• Transitive Closure it the reachability matrix to reach from vertex u to vertex
v of a graph
• A transitive closure matrix is a matrix formed by the reachability factor,
which means if one node A of the graph is reachable from another node B,
then there exists positive reachability between A and B
• This can be easily denoted by using binary denotation of 0 and 1
• Floyd Warshall Algorithm is used to find the shortest distances between
every pair of vertices in a given weighted edge Graph. Well, for finding
transitive closure, we don't need to worry about the weighted edges and
we only need to see if there is a path from a starting vertex i to an ending
vertex j
kashiram pokharel
Warshall algorithm:
• Warshall’s Algorithm
• Main idea: a path exists between two vertices i, j, iff
• there is an edge from i to j; or
• there is a path from i to j going through vertex 1; or
• there is a path from i to j going through vertex 1 and/or 2; or
• there is a path from i to j going through vertex 1, 2, and/or 3; or
..
• there is a path from i to j going through any of the other vertices
kashiram pokharel
kashiram pokharel
kashiram pokharel
kashiram pokharel
kashiram pokharel
Graph traversal:
• Graph traversal is a technique used for searching a vertex in a graph.
The graph traversal is also used to decide the order of vertices is
visited in the search process. A graph traversal finds the edges to be
used in the search process without creating loops. That means using
graph traversal we visit all the vertices of the graph without getting
into looping path.
• Type of search algorithm:
kashiram pokharel
Breadth First Search(BFS)
• Breadth-first search is the most common search strategy for traversing a tree or
graph. This algorithm searches breadthwise in a tree or graph, so it is called
breadth-first search.
• BFS algorithm starts searching from the root node of the tree and expands all
successor node at the current level before moving to nodes of next level.
• BFS traversal of a graph produces a spanning tree as final result. Spanning Tree
is a graph without loops. We use Queue data structure with maximum size of
total number of vertices in the graph to implement BFS traversal
• In this, first we take any node as a starting node then we take all the nodes
adjacent to that starting node. Similar approach we take for all other adjacent
node, which are adjacent to the starting node and so on. We maintain the status
of the visited node in one array so that no node can be traversed again.
kashiram pokharel
Algorithm:
kashiram pokharel
kashiram pokharel
kashiram pokharel
kashiram pokharel
Example2:
kashiram pokharel
kashiram pokharel
DFS(Depth First Search)
• DFS traversal of a graph produces a spanning tree as final
result. Spanning Tree is a graph without loops. We use Stack data
structure with maximum size of total number of vertices in the graph to
implement DFS traversal
kashiram pokharel
kashiram pokharel
Spanning tree:
kashiram pokharel
kashiram pokharel
kashiram pokharel
• Algorithm for generating minimum spanning tree are :
• Prism algorithm
• Kruskals algorithm
• Round robin algorithm
kashiram pokharel
Prism algorithm:
• we start with an arbitrary vertex of given graph. Let the arbitrary vertex be
V .Then we find all the adjacent vertices of V and make a set of consisting
of these vertex pair.
• we select the vertex pair with the least weight from this set and add the
vertex pair to the tree to be formed. Let this vertex pair be (u,w).
• Again we find all the adjacent pair of w and add these vertex pair to the
previous set such that a new set of pair of vertices is formed.
• Then we select a vertex pair with least weight from this newly formed set
and add it to the tree to be formed .while adding vertex pair to the tree, if
any vertex pair with minimum weight forms a cycle, we discard it and move
to the vertex pair with next minimum weight in the set.
kashiram pokharel
Find the minimum spanning tree from the
following graph.
Step 1:
• Let the arbitrary vertex be V1
• The adjacent vertex of V1 are v2,v3,v4 and v5
• Hence the set of these pair of vertices is
• (v1,v2)=5
• (v1,v3)=4
• (v1,v4)=9
• (v1,v5)=6
• Since the vertex pair (v1,v3)=4 is the vertex pair with least weight, it is
added to the tree and removed it from the set.
kashiram pokharel
Step 2:
• Now adjacent node of v3 are v1,v2,v5.Therefore, add these adjacent pair into the set
,Then the set becomes,
• (v1,v2)=5
• (v1,v4)=9
• (v1,v5)=6
• (v3,v2)=8
• (v3,v5)=12
• Since the vertex pair (v1,v2)=5 is the vertex pair with least weight, it is added to the tree
and removed it from the set.
Step 3:
• Now the adjacent node of v2 are all included in the set. Hence , choose another
minimum value from the set. Here (v1,v5)=6 is the minimum .so , (v1,v5)=6 is included in
the tree and removed from set.
• Then set becomes
• (v1,v4)=9
• (v3,v2)=8
• (v3,v5)=12 kashiram pokharel
• Step 4:
• Again the adjacent node of v5 are v3,v1,v4,v6.hence the set consisting of these adjacent
pair is
• (v1,v4)=9
• (v3,v2)=8
• (v3,v5)=12
• (v5,v4)=10
• (v5,v6)=15
• Here the adjacent pair (v3,v2)=8 is minimum but it forms the cycle.Hence discard it.
• Again the minimum weight of vertex pair is (v1,v4)=9 .which doesn,t form any cycle.Hence
it is added to the tree.
• Now the tree becomes
kashiram pokharel
• Now adjacent node of v4 are v6,v5,v1 and (v1,v4)is already in set hence
discard it. Therefore new set is
• (v3,v5)=12
• (v5,v4)=10
• (v5,v6)=15
• (v4,v6)=20
• Here ,minimum weight of vertex pair is (v5,v4)=10 which forms a cycle and
hence discard it.
• Again , minimum weight of a set is (v3,v5)=12 which agains forms a cycle
and hence discard it.
• Again minimum weight of vertex pair in a set is (v5,v6)=15 .so include this
vertex pair in a tree.
kashiram pokharel
• The adjacent node of v6 are v4 and v5 which are already in tree so
discard them.
• Now choose minimum weighted vertex pair in a set i.e. (v4,v6)=20
which also forms a cycle hence discard it.
• Now the final minimum spanning tree is
kashiram pokharel
Kruskals algorithm:
• In kruskals algorithm, we first list all the pair of vertices in ascending order
of their weights. i.e. The vertex pair with least weight is the first pair of the
list, the vertex pair with the next minimum weight is the second pair of the
list and so on.
• Then we take the vertex pair with the least minimum weight from this list
and add it to the tree to be formed .During the process of adding vertex
pair to the tree, if any vertex pair with minimum weight forms a cycle, we
discard it and moves to the vertex pair with minimum weight in the list.
• This process is continued until all the vertex pairs from the list are added to
the tree. when the list becomes empty,the resulting tree will be the
minimum spanning tree.
kashiram pokharel
Find the minimum spanning tree from the
following graph:
kashiram pokharel
• Solution:
• The list of vertex pairs in ascending order of their weight
• (V2,V3)=5
• (V1,V3)=6
• (V1,V2)=8
• (V1,V4)=10
• (V5,V7)=10
• (V4,V3)=12
• (V3,V6)=15
• (V4,V6)=20
• (V4,V5)=22
• (V5,V6)=25
• (V6,V7)=30
•
• Since ,the vertex pair (V2,V3)=5 is the pair with least weight .so it is added to the tree.
kashiram pokharel
• Step 2:
• The vertex pair in ascending order (V1,V3)=6 is minimum weight ,so it is
added to the tree
Step 3:
• The least weighted vertex pair in the list is (V1,V2)=8 but it generates
cycle.so discard it .
• Again ,the least weight vertex pair in the list is (V1,V4)=10 .so it is added
to the list.
kashiram pokharel
• Step iv:
• The least weight vertex pair in the list is (V5,V7)=10 Hence it is added
to the tree now the tree becomes
• STEP V:
• The next least weight vertex pair in the list is (V4,V3)=12 but which
generates the cycle in the list so discard it
• Again , the next least weight vertex pair in the list is (V3,V6)=15 hence
it is added to the tree.
kashiram pokharel
• STEP VI:
• The next least weighted vertex pair in the list is (V4,V6)=20 but which forms a
cycle hence discard it.
• Again, the next least weighted vertex pair is (V4,V5)=22. Hence it is added to
the tree.
• Step vii:
• The remaining weighted vertex pair in the list forms a cycle .Hence discard
them .
• Now the total weight of the tree is w=5+6+10+15+22+10=………..
kashiram pokharel
Round robin algorithm:
• It is an algorithm to find minimum spanning tree , provides the better performance
when the number of edge is low. It is similar to kruskals algorithm except that there
is a priority queue of arcs associated with each partial tree rather than the one
global priority queue of all unexamined arcs.
Algorithm:
• Initialize all the vertices in a priority queue
• Each vertex is in it’s own set called partial tree and maintained in a queue arbitrarily.
• Maintain each edge associated with each node or partial tree in priority queue ordered by the
weight of the edges
• Select a partial tree from queue and find the minimum weighted edge incident to
the partial tree from priority queue.
• Find the partial tree that is connected by minimum path edge .remove two sub tree
joining by the edge and combine into a single new tree and add to rear of the
queue.
• Combine two priority queue of the node A and B (partial tree A and B) and delete the edge
connecting them from queue.
• Repeat the algorithm until a queue contains a single tree which is called minimum spanning tree.
kashiram pokharel
Find the minimum spanning tree
using Round robin algorithm:
STEP 1:
• Insert all the nodes in the queue(sequentially or randomly)
• Queue: A, B, C, D, E, F
• Now A can be removed from the queue. Select a minimum weighted
edge of partial tree of A. Here, AB=2 is the shortest edge connected
with the node A. so B is also removed from the queue .Now ,AB is a
single partial tree and is inserted from rear of the queue.
• Queue: C,D,E,F,AB
kashiram pokharel
• C can be removed from queue. select a minimum weighted edge of
partial tree of C. CF=2 is the shortest edge connected with C. so F is
also removed from queue. Now CF is a single partial tree and is
inserted at the rear of the queue.
• Queue: D,E,AB,CF
kashiram pokharel
• D can be removed from queue. select a minimum weighted edge of
partial tree of D. Here, (D, AB)=1 is the shortest edge connected with
D. so AB also removed from queue. now, DAB is a single partial tree
and is inserted at the rear of the queue.
• Queue: E, CF, DAB
• Tree:
kashiram pokharel
• DAB partial can be removed
from queue, select a minimum
• Remove vertices E,CF and insert weighted edge of partial tree of
at rear of queue ECF DAB with another partial tree
.Here DAB,ECF=3 is the shortest
• Queue: DAB,ECF edge so, insert at the rear of
• Tree: queue ABDECF
• Queue: ABDEFC
kashiram pokharel