UNIT- 5 (DS)
UNIT- 5 (DS)
UNIT- 5 (DS)
GRAPH THEORY
Graph is a collection of vertices and arcs in which vertices are connected with
arcs
Or
Graph is a collection of nodes and edges in which nodes are connected with
edges
A graph G can be defined as an ordered set G(V, E) where V(G) represents the set
of vertices and E(G ) represents the set of edges which are used to connect these
vertices.
G=(V,E)
Where V={v1,v2,v3,...}
E={E1,E2,E3,....}
V = {A,B,C,D,E} and
E is collection of Edges
E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
Graph Terminology
We use the following terms in graph data structure...
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)).
Adjacency: If two nodes or vertices are connected to each other through an edge,
it is said to be an adjacency.
Types of graphs:
2. Directed Grpah: A directed graph is a graph in which all the edges are uni-
directional i.e. the edges point in a single direction.
1 -> 2 -> 3
1 -> 3
1 -> 4 -> 3
4) Multi Graph
A graph G=(V, E) is referred to as a multigraph if it has some parallel edges
between two vertices but doesn't contain any self-loop. An edge of a graph that
starts from a vertex and ends at the same vertex is called a loop or a self-loop.
5)Complete Graph
A simple graph G=(V, E) with n vertices is also called a complete graph if the
degree of each vertex is n-1, i.e. one vertex is attached with n-1 edges or the rest of
the vertices in the graph. A complete graph is also called a Full Graph.
6)Disconnected Graph
If in a graph G = (V, E), there does not exist any path between at least one pair of
vertices, it is a disconnected graph. A null graph with n vertices is a disconnected
graph.
7)Cyclic Graph
A graph is termed cyclic if it forms at least one cycle.
8) Acyclic Graph
A graph is said to be acyclic if it contains no cycles.
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.
Adjacency List
In this representation, every vertex of a graph contains list of its adjacent vertices.
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.
Out of Syallabus
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...
It traverses a graph in a depth-ward motion and gets the next vertex to start a
search when a dead end occurs in any iteration.
Example
Spanning Tree
There are two algorithms which are being used to find minimum spanning tree.
o Prim's Algorithm
o Kruskal's Algorithm
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.
To contrast with Kruskal's algorithm and to understand Prim's algorithm better,
we shall use the same example –
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.
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.
i.e Minimum spanning tree cost is= 7+3+3+2+2=17
Kruskal's Spanning Tree Algorithm
Kruskal's algorithm to find the minimum cost spanning tree uses the greedy
approach. This algorithm treats the graph as a forest and every node it has as an
individual tree. A tree connects to another only and only if, it has the least cost
among all available options and does not violate MST properties.
To understand Kruskal's algorithm let us consider the following example −
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).
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 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.
Minimum spanning tree cost is =7+3+3+2+2=17