0% found this document useful (0 votes)
31 views24 pages

UNIT 5@graph Thory

The document provides an introduction to graph theory, defining key concepts such as vertices, edges, and types of graphs, as well as graph representations like adjacency matrices and lists. It discusses graph traversal techniques, specifically Depth First Search (DFS) and Breadth First Search (BFS), and explains the concept of spanning trees and minimum spanning trees along with algorithms like Kruskal's and Prim's. The document emphasizes the applications of spanning trees in network planning and optimization.

Uploaded by

Khushman Grover
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views24 pages

UNIT 5@graph Thory

The document provides an introduction to graph theory, defining key concepts such as vertices, edges, and types of graphs, as well as graph representations like adjacency matrices and lists. It discusses graph traversal techniques, specifically Depth First Search (DFS) and Breadth First Search (BFS), and explains the concept of spanning trees and minimum spanning trees along with algorithms like Kruskal's and Prim's. The document emphasizes the applications of spanning trees in network planning and optimization.

Uploaded by

Khushman Grover
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction to Graphs

Graph is a non-linear data structure. It contains a set of points known as nodes (or vertices) and a

set of links known as edges (or Arcs). Here edges are used to connect the vertices. A graph is

defined as follows...
Graph is a collection of vertices and arcs in which vertices are connected with arcs

Graph is a collection of nodes and edges in which nodes are connected with edges

Generally, a graph G is represented as G = ( V , E ), where V is set of vertices and E is set of


edges.
The following is a graph with 5 vertices and 6 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.

CS WITH HARBANS SIR

Graph Terminology
We use the following terms in graph data structure...

Vertex
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.

Edge
An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is
represented as (startingVertex, endingVertex). For example, in above graph the link between
vertices A and B is represented as (A,B). In above example graph, there are 7 edges (i.e., (A,B),
(A,C), (A,D), (B,D), (B,E), (C,D), (D,E)).
Edges are three types.
1. Undirected Edge - An undirected egde is a bidirectional edge. If there is undirected edge
between vertices A and B then edge (A , B) is equal to edge (B , A).
2. Directed Edge - A directed egde is a unidirectional edge. If there is directed edge
between vertices A and B then edge (A , B) is not equal to edge (B , A).
3. Weighted Edge - A weighted egde is a edge with value (cost) on it.

Undirected Graph
A graph with only undirected edges is said to be undirected graph.

Directed Graph
A graph with only directed edges is said to be directed graph.

Mixed Graph
A graph with both undirected and directed edges is said to be mixed graph.

End vertices or Endpoints


The two vertices joined by edge are called end vertices (or endpoints) of that edge.

Origin
CS WITH HARBANS SIR
If a edge is directed, its first endpoint is said to be the origin of it.

Destination
If a edge is directed, its first endpoint is said to be the origin of it and the other endpoint is said to
be the destination of that edge.

Adjacent
If there is an edge between vertices A and B then both A and B are said to be adjacent. In other
words, vertices A and B are said to be adjacent if there is an edge between them.

Incident
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.

Parallel edges or Multiple edges


If there are two undirected edges with same end vertices and two directed edges with same origin
and destination, such edges are called parallel edges or multiple edges.

Self-loop
Edge (undirected or directed) is a self-loop if its two endpoints coincide with each other.

Simple Graph
A graph is said to be simple if there are no parallel and self-loop edges. CS WITH HARBANS SIR

Path
A path is a sequence of alternate vertices and edges that starts at a vertex and ends at other
vertex such that each edge is incident to its predecessor and successor vertex.

Graph Representations
Graph data structure is represented using following representations...

1. Adjacency Matrix

2. Incidence Matrix

3. Adjacency List
Adjacency Matrix
In this representation, the graph is represented using a matrix of size total number of
vertices by a total number of vertices. That means a graph with 4 vertices is represented
using a matrix of size 4X4. In this matrix, both rows and columns represent vertices. This
matrix is filled with either 1 or 0. Here, 1 represents that there is an edge from row vertex
to column vertex and 0 represents that there is no edge from row vertex to column
vertex.

For example, consider the following undirected graph representation...

Directed graph representation...

CS WITH HARBANS SIR

Incidence Matrix
In this representation, the graph is represented using a matrix of size total number of
vertices by a total number of edges. That means graph with 4 vertices and 6 edges is
represented using a matrix of size 4X6. In this matrix, rows represent vertices and
columns represents edges. This matrix is filled with 0 or 1 or -1. Here, 0 represents that
the row edge is not connected to column vertex, 1 represents that the row edge is
connected as the outgoing edge to column vertex and -1 represents that the row edge is
connected as the incoming edge to column vertex.

For example, consider the following directed graph representation...


Adjacency List
In this representation, every vertex of a graph contains list of its adjacent vertices.

For example, consider the following directed graph representation implemented using
linked list...

This representation can also be implemented using an array as follows..


CS WITH HARBANS SIR

Graph Traversal - DFS


Graph traversal is a technique used for a searching 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.
There are two graph traversal techniques and they are as follows...

1. DFS (Depth First Search)

2. BFS (Breadth First Search)

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 thegraph to implement
DFS traversal.

We use the following steps to implement DFS traversal...

 Step 1 - Define a Stack of size total number of vertices in the graph.

 Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on

to the Stack.


CS WITH HARBANS SIR
Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top

of stack and push it on to the stack.

 Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is

at the top of the stack.

 Step 5 - When there is no new vertex to visit then use back tracking and 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

BFS (Breadth First Search)


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.

We use the following steps to implement BFS traversal...


 Step 1 - Define a Queue of size total number of vertices in the graph.
 Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it
into the Queue.
 Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the
Queue and insert them into the Queue.
 Step 4 - When there is no new vertex to be visited from the vertex which is at front of the
Queue then delete that vertex.
 Step 5 - Repeat steps 3 and 4 until queue becomes empty.
 Step 6 - When queue becomes empty, then produce final spanning tree by removing
unused edges from the graph

Spanning tree
A spanning tree is a sub-graph (sub set) of an undirected and a
connected Graph(G), which includes all the vertices of the graph having
a minimum possible number of edges. If a vertex is missed, then it is not
a spanning tree. Hence, a spanning tree does not have cycles and it
cannot be disconnected.

If all the vertices are connected in a graph, then there exists at least one
spanning tree. In a graph, there may exist more than one spanning tree.
CS WITH HARBANS SIR
An undirected graph is a graph in which the edges do not point in any
direction (ie. the edges are bidirectional).

A connected graph is a graph in which there is always a path from a


vertex to any other vertex.
The edges may or may not have weights assigned to them.

The total number of spanning trees with ” n “ vertices that can be


created from a complete graph is equal to n(n-2) .

If we have n = 4, the maximum number of possible spanning trees is


equal to 44-2 = 16. Thus, 16 spanning trees can be formed from a
complete graph with 4 vertices.

CS WITH HARBANS SIR

General Properties of Spanning Tree:

We now understand that one graph can have more than one spanning
tree. Following are a few properties of the spanning tree connected to
graph G −
 A connected graph G can have more than one spanning tree.
 All possible spanning trees of graph G, have the same number of
edges and vertices.
 The spanning tree does not have any cycle (loops).
 Removing one edge from the spanning tree will make the graph
disconnected, i.e. the spanning tree is minimally connected.
 Adding one edge to the spanning tree will create a circuit or loop,
i.e. the spanning tree is maximally acyclic.
Mathematical Properties of Spanning Tree
 Spanning tree has n-1 edges, where n is the number of nodes
(vertices).
 From a complete graph, by removing maximum e - n + 1 edges,
we can construct a spanning tree.
 A complete graph can have maximum nn-2 number of spanning
trees.
Thus, we can conclude that spanning trees are a subset of connected
Graph G and disconnected graphs do not have spanning tree.
Application of Spanning Tree :
Spanning tree is basically used to find a minimum path to connect all
nodes in a graph. Common application of spanning trees are −
CS WITH HARBANS SIR
 Civil Network Planning
 Computer Network Routing Protocol
 Cluster Analysis
Let us understand this through a small example. Consider, city network
as a huge graph and now plans to deploy telephone lines in such a way
that in minimum lines we can connect to all city nodes.

Minimum Spanning Tree :


A minimum spanning tree is a spanning tree in which the sum of the
weight of the edges is as minimum as possible.
In a weighted graph, a minimum spanning tree is a spanning tree that has
minimum weight than all other spanning trees of the same graph. In real-
world situations, this weight can be measured as distance, congestion,
traffic load or any arbitrary value denoted to the edges.
Graph(G)

MST of spanning tree are following

The minimum spanning tree from the above spanning trees is:
CS WITH HARBANS SIR

Minimum Spanning tree Applications :


1.To find paths in the map

2.To design networks like telecommunication networks, water supply


networks, and electrical grids.

Minimum Spanning-Tree Algorithm:


We shall learn about two most important spanning tree algorithms here
 Kruskal's Algorithm
 Prim's Algorithm

Kruskal's Algorithm :
Kruskal's algorithm is a minimum spanning tree algorithm that takes a
graph as input and finds the subset of the edges of that graph which
 form a tree that includes every vertex
 has the minimum sum of weights among all the trees that can be
formed from the graph

How Kruskal's algorithm works


We start from the edges with the lowest weight and keep adding edges
until we reach our goal.
CS WITH HARBANS SIR
The steps for implementing Kruskal's algorithm are as follows:

1. Sort all the edges from low weight to high


2. Take the edge with the lowest weight and add it to the spanning tree. If
adding the edge created a cycle, then reject this edge.
3. Keep adding edges until we reach all vertices.

Note:
1.Construct Min heap with ‘e’ edge

2. Take one by one edge and add in spanning tree (cycle should not be
created)
* Best case (n-1) edge
* Worst case ‘e’ edges

To Understand Kruskal's algorithm let us consider the following


example −

Step 1 - Remove all loops and Parallel Edges


CS WITH HARBANS SIR
Remove all loops and parallel edges from the given graph.

In case of parallel edges, keep the one which has the least cost
associated and remove all others.
Step 2 - Arrange all edges in their increasing order of weight

The next step is to create a set of edges and weight, and arrange them in
an ascending order of weightage (cost).

Step 3 - Add the edge which has the least weightage CS WITH HARBANS SIR

Now we start adding edges to the graph beginning from the one which
has the least weight. Throughout, we shall keep checking that the
spanning properties remain intact. In case, by adding one edge, the
spanning tree property does not hold then we shall consider not to
include the edge in the graph.

The least cost is 2 and edges involved are B,D and D,T. We add them.
Adding them does not violate spanning tree properties, so we continue
to our next edge selection.
Next cost is 3, and associated edges are A,C and C,D. We add them
again −

Next cost in the table is 4, and we observe that adding it will create a
circuit in the graph. −

CS WITH HARBANS SIR


We ignore it. In the process we shall ignore/avoid all edges that create a
circuit.

We observe that edges with cost 5 and 6 also create circuits. We ignore
them and move on.
Now we are left with only one node to be added. Between the two least
cost edges available 7 and 8, we shall add the edge with cost 7.

By adding edge S,A we have included all the nodes of the graph and we
now have minimum cost spanning tree. CS WITH HARBANS SIR

Kruskal's Algorithm Complexity


The time complexity Of Kruskal's Algorithm is: O(E log E).
Kruskal's Algorithm Applications
 In order to layout electrical wiring
 In computer network (LAN connection)
Prim's Algorithm :
Prim's algorithm to find minimum cost spanning tree (as Kruskal's
algorithm) uses the greedy approach. Prim's algorithm shares a
similarity with the shortest path first algorithms.
Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes
as a single tree and keeps on adding new nodes to the spanning tree
from the given graph.
The steps for implementing Prim's algorithm are as follows:

1. Initialize the minimum spanning tree with a vertex chosen at random.


2. Find all the edges that connect the tree to new vertices, find the
minimum and add it to the tree
3. Keep repeating step 2 until we get a minimum spanning tree

To contrast with Kruskal's algorithm and to understand Prim's algorithm


better, we shall use the same example −

CS WITH HARBANS SIR

Step 1 - Remove all loops and parallel edges


Remove all loops and parallel edges from the given graph. In case of
parallel edges, keep the one which has the least cost associated and
remove all others.

Step 2 - Choose any arbitrary node as root node

In this case, we choose S node as the root node of Prim's spanning tree.
This node is arbitrarily chosen, so any node can be the root node. One
may wonder why any video can be a root node. So the answer is, in the
spanning tree all the nodes of a graph are included and because it is
connected then there must be at least one edge, which will join it to the
CS WITH HARBANS SIR
rest of the tree.

Step 3 - Check outgoing edges and select the one with less cost

After choosing the root node S, we see that S,A and S,C are two edges
with weight 7 and 8, respectively. We choose the edge S,A as it is lesser
than the other.

Now, the tree S-7-A is treated as one node and we check for all edges
going out from it. We select the one which has the lowest cost and
include it in the tree.
After this step, S-7-A-3-C tree is formed. Now we'll again treat it as a
node and will check all the edges again. However, we will choose only
the least cost edge. In this case, C-3-D is the new edge, which is less
than other edges' cost 8, 6, 4, etc.

CS WITH HARBANS SIR


After adding node D to the spanning tree, we now have two edges going
out of it having the same cost, i.e. D-2-T and D-2-B. Thus, we can add
either one. But the next step will again yield edge 2 as the least cost.
Hence, we are showing a spanning tree with both edges included.

We may find that the output spanning tree of the same graph using two
different algorithms is same.
Prim's Algorithm Complexity
The time complexity of Prim's algorithm is O(E log V).
Prim's Algorithm Application
 Laying cables of electrical wiring
 In network designed
 To make protocols in network cycles

Prim's vs Kruskal's Algorithm

Prim’s Kruskal’s

This algorithm is for obtaining minimum This algorithm is for obtaining minimum
spanning tree by selecting the adjacent spanning tree but it is not necessary to choose
vertices of already selected vertices. adjacent vertices of already selected vertices.
CS WITH HARBANS SIR

Prim’s algorithm initializes with a Kruskal’s algorithm initiates with an edge


(root)node

Prim’s algorithms span from one node to Kruskal’s algorithm select the edges in a way that
another the position of the edge is not based on the last
step

In prim’s algorithm, graph must be a Kruskal’s can function on disconnected graphs


connected graph too.

Prim’s algorithm has a time complexity of Kruskal’s time complexity is O(logV).


O(V2)
Shortest Path Algorithm :

In graph theory, the shortest path problem is the problem of finding


a path between two vertices (or nodes) in a graph such that the sum of
the weights of its constituent edges is minimized.

There are also different types of shortest path algorithms. Maybe you
need to find the shortest path between point A and B, but maybe you
need to shortest path between point A and all other points in the graph.

Shortest path algorithms have many applications. As noted CSearlier,


WITH HARBANS SIR
mapping software like Google or Apple maps makes use of shortest path
algorithms. They are also important for road network, operations, and
logistics research. Shortest path algorithms are also very important for
computer networks, like the Internet.

Any software that helps you choose a route uses some form of a shortest
path algorithm. Google Maps, for instance, has you put in a starting
point and an ending point and will solve the shortest path problem for
you.
Types of Shortest Path Algorithms:

There are two main types of shortest path algorithms, single-source and
all-pairs. Both types have algorithms that perform best in their own way.
All-pairs algorithms take longer to run because of the added complexity.
All shortest path algorithms return values that can be used to find the
shortest path, even if those return values vary in type or form from
algorithm to algorithm.
1.Single-source :

Single-source shortest path algorithms operate under the following


principle:

Given a graph G, with vertices V, edges E with weight function w(u, v)


= wu,v, and a single source vertex s, return the shortest paths from s to
all other vertices in V.

If the goal of the algorithm is to find the shortest path between only two
given vertices, s and t, then the algorithm can simply be stopped when
that shortest path is found. Because there is no way to decide which
vertices to "finish" first, all algorithms that solve for the shortest path
between two given vertices have the same worst-case asymptotic
complexity as single-source shortest path algorithms.

This paradigm also works for the single-destination shortest


path problem. By reversing all of the edges in a graph, the single-
CS WITHSo,
destination problem can be reduced to the single-source problem. HARBANS SIR
given a destination vertex t, this algorithm will find the shortest
paths starting at all other vertices and ending at t.

2.All-pairs :

All-pairs shortest path algorithms follow this definition:

Given a graph G, with vertices V, edges E with weight function w(u, v)


= wu,v return the shortest path from u to v for all (u,v) in V.

The most common algorithm for the all-pairs problem is the floyd-
warshall algorithm. This algorithm returns a matrix of values M, where
each cell Mi,j is the distance of the shortest path from vertex i to vertex j.
Path reconstruction is possible to find the actual path taken to achieve
that shortest path, but it is not part of the fundamental algorithm.
Algorithms:

Bellman-Ford algorithm

The Bellman-Ford algorithm solves the single-source problem in the


general case, where edges can have negative weights and the graph is
directed. If the graph is undirected, it will have to modified by including
two edges in each direction to make it directed.

Bellman-Ford has the property that it can detect negative weight cycles
reachable from the source, which would mean that no shortest path
exists. If a negative weight cycle existed, a path could run infinitely on
that cycle, decreasing the path cost to infty ∞.

If there is no negative weight cycle, then Bellman-Ford returns the


weight of the shortest path along with the path itself.

Dijkstra's algorithm
CS WITH
Dijkstra's algorithm makes use of breadth-first search (which is notHARBANS
a SIR
single source shortest path algorithm) to solve the single-source
problem. It does place one constraint on the graph: there can be no
negative weight edges. However, for this one constraint, Dijkstra greatly
improves on the runtime of Bellman-Ford.

Dijkstra's algorithm is also sometimes used to solve the all-pairs shortest


path problem by simply running it on all vertices in V. Again, this
requires all edge weights to be positive.

Topological Sort algorithm

For graphs that are directed acyclic graphs (DAGs), a very useful tool
emerges for finding shortest paths. By performing a topological sort on
the vertices in the graph, the shortest path problem becomes solvable in
linear time.
A topological sort is an ordering all of the vertices such that for each
edge (u,v) in E, u comes before v in the ordering. In a DAG, shortest
paths are always well defined because even if there are negative weight
edges, there can be no negative weight cycles.

Floyd-Warshall algorithm

The Floyd-Warshall algorithm solves the all-pairs shortest path problem.


It uses a dynamic programming approach to do so. Negative edge weight
may be present for Floyd-Warshall.

Floyd-Warshall takes advantage of the following observation: the


shortest path from A to C is either the shortest path from A to B plus the
shortest path from B to C or it's the shortest path from A to C that's
already been found. This may seem trivial, but it's what allows Floyd-
Warshall to build shortest paths from smaller shortest paths, in the
classic dynamic programming way.

Johnson's Algorithm CS WITH HARBANS SIR

While Floyd-Warshall works well for dense graphs (meaning many


edges), Johnson's algorithm works best for sparse graphs (meaning few
edges). In sparse graphs, Johnson's algorithm has a lower asymptotic
running time compared to Floyd-Warshall.

Johnson's algorithm takes advantage of the concept of reweighting, and


it uses Dijkstra's algorithm on many vertices to find the shortest path
once it has finished reweighting the edges.

Comparison of Algorithms:

Algorithm Runtime
Bellman-Ford O(|V| . |E|)
2
Dijkstra's (with list) O(|V| )
Topological Sort O(|V| + |E|)

Floyd-Warshall O(|V|3)
Johnson's *O(|E| . |V| + |V|2 .log2(|V|))

CS WITH HARBANS SIR

You might also like