UNIT 5@graph Thory
UNIT 5@graph Thory
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
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.
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.
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.
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 implemented using
linked list...
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...
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
Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is
Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex
Step 7 - When stack becomes Empty, then produce final spanning tree by removing
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).
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.
The minimum spanning tree from the above spanning trees is:
CS WITH HARBANS SIR
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
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
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. −
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
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.
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 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 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
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.
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 :
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.
2.All-pairs :
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
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 ∞.
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.
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
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|))