Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

UNIT- 5 (DS)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

UNIT-V

GRAPH THEORY

Graph: 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
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,....}

Ex: The following is a graph with 5 vertices and 7 edges.


This graph G can be defined as G = ( V , E )
Where: V is collection of vertices

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

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

Path: It represents a sequence of edges between the two vertices.

EX: Path (A to E) is A-B-E (OR) A-D-E (OR) A-C-D-E etc

Adjacency: If two nodes or vertices are connected to each other through an edge,
it is said to be an adjacency.

Ex: A is Adjacency Nodes are B,C,D

Types of graphs:

1. Undirected Graph : An undirected graph is a graph in which all the edges


are bi-directional i.e. the edges do not point in any specific direction.

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.

3. Weighted Graph: In a weighted graph, each edge is assigned a weight or


cost. Consider a graph of 4 nodes as in the diagram below. As you can see
each edge has a weight/cost assigned to it. If you want to go from vertex 1 to
vertex 3, you can take one of the following 3 paths:

 1 -> 2 -> 3
 1 -> 3
 1 -> 4 -> 3

Therefore the total cost of each path will be as follows: -


The total cost of 1 -> 2 -> 3 will be (1 + 2) i.e. 3 units

The total cost of 1 -> 3 will be 1 unit

The total cost of 1 -> 4 -> 3 will be (3 + 2) i.e. 5 units

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.

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

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

Disadvantages of Adjacency List


 It is not easy for adding or removing an edge to/from adjacent list.
 It does not allow to make an efficient implementation, if dynamically change of
vertices number is required.

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


Real-life applications of graphs:

 Airline Planning (Flow problems)


 Neural Networks
 Social Networks
 Blockchains
 Map instructions (Shortest path)
 completing the Sudoku problems (Graph coloring)
 algorithms for search engines (PageRank algorithm)
 Use of social media (Community detection)

Out of Syallabus

Graph Traversal (Operations):

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)


 Depth first search (DFS) is used for traversing a finite graph.
 DFS traverses the depth of any particular path before exploring its breadth.
 It explores one subtree before returning to the current node and then exploring the
other subtree.
 DFS uses stack instead of queue.

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

 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.
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.
 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)
 Breadth first search is used for traversing a finite graph.
 It visits the neighbor vertices before visiting the child vertices.
 BFS uses a queue for search process and gets the next vertex to start a search
when a dead end occurs in any iteration.
 It traverses a graph in a breadth-ward motion.
 It is used to find the shortest path from one vertex to another.
 The main purpose of BFS is to traverse the graph as close as possible to the root
node.
 BFS is a different approach for traversing the graph nodes.
 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

Example
Spanning Tree

Spanning tree can be defined as a sub-graph of connected, undirected graph G that


is a tree produced by removing the desired number of edges from a graph. In other
words, Spanning tree is a non-cyclic sub-graph of a connected and undirected
graph G that connects all the vertices together. A graph G can have multiple
spanning trees.

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 −
 Civil Network Planning
 Computer Network Routing Protocol
 Cluster Analysis
Minimum Spanning Tree(MST)
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.

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

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


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

You might also like