DS Unit 5
DS Unit 5
Graph ADT
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
A graph is said to be null if there are no edges in that graph. A pictorial representation
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.
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.
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.
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.
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 graph is said to be a multigraph if there exist two or more than two edges between any pair of nodes in
thegraph.
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.
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
DAG:
A directed acyclic graph (DAG) is a graph that has directed edges and has no cycles connecting the other edges.
TOPOLOGICAL ORDERING
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:
Dijkstra’s algorithm
Warshall’s algorithm
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:
2. Prim algorithm.
Kruskal’s Algorithm
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.
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 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 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 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,
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.