0% found this document useful (0 votes)
21 views

DS Unit 5

This document is more important to research and development of the future

Uploaded by

harinikadevi9
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)
21 views

DS Unit 5

This document is more important to research and development of the future

Uploaded by

harinikadevi9
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/ 17

UNIT 5

Graph ADT

Basic Graph Concepts:


Graph is a data structure that consists of following two components:
1. A finite set of vertices also called as nodes.
2. A finite set of ordered pair of the form (u, v) called as edge.

In the figure below, we have a simple graph where there are five nodes in total and six edges.

The nodes in any graph can be referred to as entities and the edges that connect different nodes
define the relationships between these entities. In the above graph we have a set of nodes {V} =
{A, B, C, D,E} and a set of edges, {E} = {A-B, A-D, B-C, B-D, C-D, D-E} respectively

Types of Graphs

Let's cover various different types of graphs.


1. Null Graphs

A graph is said to be null if there are no edges in that graph. A pictorial representation

of the null graph is given below:

2. Undirected Graphs
3. Directed Graphs

4. Cyclic Graph

A graph that contains at least one node that traverses back to itself is known as a cyclic graph. In simple
words, a graph should have at least one cycle formation for it to be called a cyclic graph.

It can be easily seen that there exists a cycle between the nodes (a, b, c) and hence it is a cyclic graph.

5. Acyclic Graph

A graph where there's no way we can start from one node and can traverse back to the same one, or
simply doesn't have a single cycle is known as an acyclic graph.

A pictorial representation of an acyclic graph is given below:

6. Weighted Graph

When the edge in a graph has some weight associated with it, we call that graph as a weighted graph. The
weight is generally a number that could mean anything, totally dependent on the relationship between the
nodes of that graph.

A pictorial representation of the weighted graph is given below:


It can also be noted that if any graph doesn't have any weight associated with it, we simply call it an
unweighted graph.

7. Connected Graph

A graph where we have a path between every two nodes of the graph is known as a connected graph. A
path here means that we are able to traverse from a node "A" to say any node "B". In simple terms, we
can say that if we start from one node of the graph we will always be able to traverse to all the other
nodes of the graph from that node, hence the connectivity.

A pictorial representation of the connected graph is given below:

8. Disconnected Graph

A graph that is not connected is simply known as a disconnected graph. In a disconnected graph, we will
not be able to find a path from between every two nodes of the graph.

A pictorial representation of the disconnected graph is given below:

9. Complete Graph

A graph is said to be a complete graph if there exists an edge for every pair of vertices(nodes) of that

graph.

A pictorial representation of the complete graph is given below:


10. Multigraph

A graph is said to be a multigraph if there exist two or more than two edges between any pair of nodes in
thegraph.

A pictorial representation of the multigraph is given below:

Graph and its representations:


Graphs are used to represent many real life applications: Graphs are used to represent networks. The networks may
include paths in a city or telephone network or circuit network. Graphs are also used in social networks like
linkedIn, facebook. For example, in facebook, each person is represented with a vertex(or node).
Following two are the most commonly used representations of graph.
1. Adjacency Matrix
2. Adjacency List
There are other representations also like, Incidence Matrix and Incidence List. The choice of the graph
representation is situation specific. It totally depends on the type of operations to be performed and ease of use.
Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be
adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Adjacency matrix for undirected
graph is always symmetric. Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then there
is an edge from vertex i to vertex j with weight w.
The adjacency matrix for the above example graph is:
Adjacency List:
An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be array[]. An entry
array[i] represents the linked list of vertices adjacent to the ith vertex. This representation can also be used to
represent a weighted graph. The weights of edges can be stored in nodes of linked lists.
Following is adjacency list representation of the above graph.

GRAPH TRAVERSALS

1. BFS
2. DFS
Example :

For the above graph the Breadth first traversal sequence is: A F C B D E G J K.
For the above graph the Depth first traversal sequence is: A F D J K G E C B.

Algorithm: Breadth-First Search Traversal

BFS(V, E, s)
for each u in V − {s}
do color[u] ← WHITE
d[u] ← infinity
π[u] ← NIL
color[s] ← GRAY
d[s] ← 0
π[s] ← NIL
Q ← {}
ENQUEUE(Q, s)
while Q is non-empty
do u ← DEQUEUE(Q)
for each v adjacent to u
do if color[v] ← WHITE
then color[v] ← GRAY
d[v] ← d[u] + 1
π[v] ← u
ENQUEUE(Q, v)
DEQUEUE(Q)
color[u] ← BLACK

Algorithm for DFS

DAG:

A directed acyclic graph (DAG) is a graph that has directed edges and has no cycles connecting the other edges.
TOPOLOGICAL ORDERING

 Topological ordering is the ordering of vertices based on a criterion.


 If there is an edge from i to j then I must be visited before j.
 Topological ordering is possible only in directed acyclic graph.

Algorithm:
 Create a list of in-degree of each vertex
 Initiate counter to 0
 If a vertex has in degree 0 then push to stack
 When the stack is not empty, pop the element from stack.
 Add the element in to the topological sorted list with the counter. Then increment the counter by 1
 Foe every vertex adjacent to this vertex ,decrement the in degree by 1
 If any of the adjacent vertex new in degree becomes 0 push it in to stack.
 Repeat the procedure until all vertex indegree becomes 0.

Example:
SHORTEST PATH ALGORITHMS

In this section, we will discuss three different algorithms to calculate the shortest path between the vertices of a
graph G. These algorithms include:

Minimum spanning tree

Dijkstra’s algorithm

Warshall’s algorithm

Minimum Spanning Tree (MST):

A spanning tree for a connected graph is a tree whose vertex set is the same as the vertex set of the given graph, and
whose edge set is a subset of the edge set of the given graph. i.e., any connected graph will have a spanning tree.

Weight of a spanning tree w(T) is the sum of weights of all edges in T. Minimum spanning tree (MST) is a
spanning tree with the smallest possible weight.

Minimum spanning tree, can be constructed using any of the following two algorithms:

1. Kruskal’s algorithm and

2. Prim algorithm.
Kruskal’s Algorithm

 This is a greedy algorithm


 Create a tree from graph by selecting the edges such that their total weight is minimum.
 Weighted and undirected graph

Kruskal’s Algorithm for minimal spanning tree is as follows:

1. Make the tree T empty.

2. Repeat the steps 3, 4 and 5 as long as T contains less than n – 1 edges and E is not empty otherwise, proceed to
step 6.

3. Choose an edge (v, w) from E of lowest cost.

4. Delete (v, w) from E.

5. If (v, w) does not create a cycle in T

then Add (v, w) to T

else discard (v, w)

6. If T contains fewer than n - 1 edges then print no spanning tree.


Prims Algorithm

Prim’s algorithm is a greedy algorithm that is used to form a minimum spanning tree for a connected weighted
undirected graph. In other words, the algorithm builds a tree that includes every vertex and a subset of the edges in
such a way that the total weight of all the edges in the tree is minimized.

For this, the algorithm maintains three sets of vertices which can be given as below:

 Tree vertices Vertices that are a part of the minimum spanning tree T.
 Fringe vertices Vertices that are currently not a part of T, but are adjacent to some tree vertex.
 Unseen vertices Vertices that are neither tree vertices nor fringe vertices fall under this category.

Step 1: Choose a starting vertex A.

Step 2: Add the fringe vertices (that are adjacent to A). The edges connecting the vertex and fringe vertices are
shown with dotted lines.

Step 3: Select an edge connecting the tree vertex and the fringe vertex that has the minimum weight and add the
selected edge and the vertex to the minimum spanning tree T. Since the edge connecting A and C has less weight,
add C to the tree. Now C is not a fringe vertex but a tree vertex.

Step 4: Add the fringe vertices (that are adjacent to C).

Step 5: Select an edge connecting the tree vertex and the fringe vertex that has the minimum weight and add the
selected edge and the vertex to the minimum spanning tree T. Since the edge connecting C and B has less weight,
add B to the tree. Now B is not a fringe vertex but a tree vertex.

Step 6: Add the fringe vertices (that are adjacent to B).


Step 7: Select an edge connecting the tree vertex and the fringe vertex that has the minimum weight and add the
selected edge and the vertex to the minimum spanning tree T. Since the edge connecting B and D has less weight,
add D to the tree. Now D is not a fringe vertex but a tree vertex.

Step 8: Note, now node E is not connected, so we will add it in the tree because a minimum spanning tree is one in
which all the n nodes are connected with n–1 edges that have minimum weight. So, the minimum spanning tree can
now be given as,

The Total cost is 27

Dijkstra’s algorithm

Dijkstra’s algorithm, given by a Dutch scientist Edsger Dijkstra in 1959, is used to find the shortest path tree. This
algorithm is widely used in network routing protocols, most notably IS-IS and OSPF (Open Shortest Path First).
Given a graph G and a source node A, the algorithm is used to find the shortest path (one having the lowest cost)
between A (source node) and every other node. Moreover, Dijkstra’s algorithm is also used for finding the costs of
the shortest paths from a source node to a destination node.

You might also like