CENG205 W-11b
CENG205 W-11b
Data structure
In this chapter, we will discuss another non-linear data structure called graphs.
Begins at the root node and explores all the neighbouring nodes.
We start examining the node A and then all the neighbours of A are
examined.
This is accomplished by using a queue that will hold the nodes that are
waiting for further processing and a variable STATUS to represent the
current state of the node.
Example 13.1
Consider the graph G and the adjacency list are given in Figure
Assume that G represents the daily flights between different cities
We want to fly from city A to I with minimum stops.
That is, find the minimum path P from A to I given that every edge has a length of 1.
Solution
As I is our final destination, we stop the execution of this algorithm as soon as it is encountered and added to the
QUEUE.
Now, backtrack from I using ORIG to find the minimum path P. Thus, we have P as A -> C -> G -> I.
Applications of Breadth-First Search Algorithm
Consider the graph G given and the adjacency list of G is also given as in the figure
Suppose we want to print all the nodes that can be reached from the node H (including H itself).
B - Pop and print the top element of the STACK, that is, H. Push all
the neighbours of H onto the stack that are in the ready state. The
STACK now becomes
C - Pop and print the top element of the STACK, that is, I. Push all the
neighbours of I onto the stack that are in the ready state. The STACK now
becomes
Example 13.2
D - Pop and print the top element of the STACK, that is, F. Push all the
neighbours of F onto the stack that are in the ready state. (Note F has
two neighbours, C and H. But only C will be added, as H is not in the
ready state.) The STACK now becomes
E - Pop and print the top element of the STACK, that is, C. Push all the
neighbours of C onto the stack that are in the ready state. The STACK
now becomes
Example 13.2
F - Pop and print the top element of the STACK, that is, G. Push all the
neighbours of G onto the stack that are in the ready state. Since there
are no neighbours of G that are in the ready state,no push operation is
performed. The STACK now becomes
G - Pop and print the top element of the STACK, that is, B. Push all the
neighbours of B onto the stack that are in the ready state. Since there
are no neighbours of B that are in the ready state, no push operation is
performed. The STACK now becomes
Example 13.2
H - Pop and print the top element of the STACK, that is, E. Push all the
neighbours of E onto the stack that are in the ready state. Since there
are no neighbours of E that are in the ready state, no push operation is
performed. The STACK now becomes empty.
Since the STACK is now empty, the depth-first search of G starting at node H is complete and the nodes which were
printed are:
H, I, F, C, G, B, E
These are the nodes which are reachable from the node H.
Applications of Depth-First Search Algorithm
While the first two use an adjacency list to find the shortest path, Warshall’s algorithm uses an
adjacency matrix to do the same.
13.8.1 Minimum Spanning Trees
A spanning tree of a connected, undirected graph G is a sub-graph of G which is a tree that connects
all the vertices together.
A minimum spanning tree (MST) is defined as a spanning tree with weight less than or equal to the weight of
every other spanning tree.
MST
Applications of Minimum Spanning Trees
1. MSTs are widely used for designing networks. A minimum spanning tree is used to determine the least
costly paths with no cycles in this network, thereby providing a connection that has the minimum cost
involved.
2. MSTs are used to find airline routes. While the vertices in the graph denote cities, edges represent the routes
between these cities. MSTs are used to optimize airline routes by finding the least costly path with no cycles.
3. MSTs are also used to find the cheapest way to connect terminals, such as cities, electronic components or
computers via roads, airlines, railways, wires or telephone lines.
4. MSTs are applied in routing algorithms for finding the most efficient path.
13.8.2 Prim’s Algorithm
Prim’s algorithm is aa algorithm that is used to form a minimum spanning tree for a connected weighted undirected graph.
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 thiscategory.
Example 13.7 Construct a minimum spanning tree of the graph given in Fig.
Example
13.8.3 Kruskal’s Algorithm
Kruskal’s algorithm is used to find the minimum spanning tree for a connected weighted graph.
The algorithm aims to find a subset of the edges that forms a tree that includes every vertex.
The total weight of all the edges in the tree is minimized.
However, if the graph is not connected, then it finds a minimum spanning forest.
Note that a forest is a collection of trees.
Similarly, a minimum spanning forest is a collection of minimum spanning trees.
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure
A
1
B C D
E F
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure
A
1
B C D
E F
2
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure
A
1
B C D
3
E F
2
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure
A
1
B C D
4
3
E F
2
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure
A
1
B C D
4
3
E F
2
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure
A
1
B C D
4
3
E F
2
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure
A
7 1
B C D
4
3
E F
2
Example 13.9 Apply Kruskal’s algorithm on the graph given in Figure
A
7 1
B C D
4
3
E F
2
13.8.4 Dijkstra’s Algorithm
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. Edsger Wybe Dijkstra
1930 - 2002
Algorithm
Dijkstra’s algorithm is used to find the length of an optimal path between two nodes in a graph.
The term optimal can mean anything, shortest, cheapest, or fastest.
If we start the algorithm with an initial node, then the distance of a node Y can be given as the distance from the
initial node to that node.
Difference between Dijkstra’s Algorithm and Minimum Spanning Tree
Minimum spanning tree algorithm is used to traverse a graph in the most efficient manner
Dijkstra’s algorithm calculates the distance from a given vertex to every other vertex in the graph.
Dijkstra’s algorithm is very similar to Prim’s algorithm. Both the algorithms begin at a specific
node and extend outward within the graph, until all other nodes in the graph have been reached.
The point where these algorithms differ is that while Prim’s algorithm stores a minimum cost
edge, Dijkstra’s algorithm stores the total cost from a source node to the current node.
Moreover, Dijkstra’s algorithm is used to store the summation of minimum cost edges, while Prim’s algorithm
stores at most one minimum cost edge.
Next Lesson
Chapter14: Searching and Sorting