3. What is a Graph?
Graphs are one of the most interesting data structures in computer
science. Graphs and the trees are somewhat similar by their structure. In
fact, tree is derived from the graph data structure. However there are
two important differences between trees and graphs.
Unlike trees, in graphs, a node can have many parents.
The link between the nodes may have values or weights.
Graphs are good in modeling real world problems like representing
cities which are connected by roads and finding the paths between
cities, modeling air traffic controller system, etc. These kinds of
problems are hard to represent using simple tree structures. The
following example shows a very simple graph.
4. In the above graph, A,B,C,D,E,F are called nodes and the connecting lines between
these nodes are called edges. The edges can be directed edges which are shown
by arrows; they can also be weighted edges in which some numbers are assigned
to them. Hence, a graph can be a directed/undirected and weighted/un-weighted
graph. In this article, we will discuss undirected and un-weighted graphs.
5. Graph Traversal
The breadth first search (BFS) and the depth first search (DFS) are
the two algorithms used for traversing and searching a node in a
graph. They can also be used to find out whether a node is reachable
from a given node or not.
6. Breadth First Search (BFS)
This is a very different approach for traversing the graph nodes. The aim of
BFS algorithm is to traverse the graph as close as possible to the root node.
Queue is used in the implementation of the breadth first search. Let’s see
how BFS traversal works with respect to the following graph:
7. If we do the breadth first traversal of the above graph and print the visited node
as the output, it will print the following output. “A B C D E F”. The BFS visits the
nodes level by level, so it will start with level 0 which is the root node, and then
it moves to the next levels which are B, C and D, then the last levels which are E
and F.
Algorithmic Steps
Step 1: Push the root node in the Queue.
Step 2: Loop until the queue is empty.
Step 3: Remove the node from the Queue.
Step 4: If the removed node has unvisited child nodes, mark them as visited
and insert the unvisited children in the queue.
Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion
and uses a queue to remember to get the next vertex to start a search, when a
dead end occurs in any iteration.
8. As in the example given above, BFS algorithm traverses from A to B to E to F
first then to C and G lastly to D. It employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert
it in a queue.
Rule 2 − If no adjacent vertex is found, remove the first vertex from the
queue.
Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
10. 3 We then see an unvisited adjacent
node from S. In this example, we
have three nodes but alphabetically
we choose A, mark it as visited and
enqueue it.
4
Next, the unvisited adjacent node
from S is B. We mark it as visited
and enqueue it.
11. 5
Next, the unvisited adjacent node
from S is C. We mark it as visited
and enqueue it.
6
Now, S is left with no unvisited
adjacent nodes. So, we dequeue
and find A.
12. 7
From A we have D as unvisited
adjacent node. We mark it as visited
and enqueue it.
At this stage, we are left with no
unmarked (unvisited) nodes. But as per
the algorithm we keep on dequeuing in
order to get all unvisited nodes. When
the queue gets emptied, the program is
over.
13. Depth First Search (DFS)
The aim of DFS algorithm is to traverse the graph in such a way that it tries
to go far from the root node. Stack is used in the implementation of the
depth first search. Let’s see how depth first search works with respect to
the following graph:
14. As stated before, in DFS, nodes are visited by going through the depth of the
tree from the starting node. If we do the depth first traversal of the above
graph and print the visited node, it will be “A B E F C D”. DFS visits the root
node and then its children nodes until it reaches the end node, i.e. E and F
nodes, then moves up to the parent nodes.
Algorithmic Steps
Step 1: Push the root node in the Stack.
Step 2: Loop until stack is empty.
Step 3: Peek the node of the stack.
Step 4: If the node has unvisited child nodes, get the unvisited child node,
mark it as traversed and push it on stack.
Step 5: If the node does not have any unvisited child nodes, pop the node
from the stack.
15. Depth First Search (DFS) algorithm traverses a graph in a depthward motion and
uses a stack to remember to get the next vertex to start a search, when a dead end
occurs in any iteration.
As in the example given above, DFS algorithm traverses from S to A to D to G to E to
B first, then to F and lastly to C. It employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a
stack.
Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop
up all the vertices from the stack, which do not have adjacent vertices.)
Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
16. Step Traversal Description
1
Initialize the stack.
2 Mark S as visited and put it onto the
stack. Explore any unvisited adjacent
node from S. We have three nodes and
we can pick any of them. For this
example, we shall take the node in an
alphabetical order.
17. 3 Mark A as visited and put it onto the
stack. Explore any unvisited adjacent
node from A. Both Sand D are
adjacent to A but we are concerned
for unvisited nodes only.
4 Visit D and mark it as visited and put
onto the stack. Here, we
have B and C nodes, which are
adjacent to D and both are unvisited.
However, we shall again choose in an
alphabetical order.
18. 5
We choose B, mark it as visited and
put onto the stack. Here B does not
have any unvisited adjacent node. So,
we pop Bfrom the stack.
6
We check the stack top for return to
the previous node and check if it has
any unvisited nodes. Here, we
find D to be on the top of the stack.
19. 7
Only unvisited adjacent node is
from D is C now. So we visit C, mark it
as visited and put it onto the stack.
As C does not have any unvisited
adjacent node so we keep popping the
stack until we find a node that has an
unvisited adjacent node. In this case,
there's none and we keep popping until
the stack is empty.
20. Spanning tree:
A spanning tree is a subset of Graph G, which has all the vertices
covered with minimum possible number of edges. Hence, a
spanning tree does not have cycles and it cannot be disconnected..
By this definition, we can draw a conclusion that every connected
and undirected Graph G has at least one spanning tree. A
disconnected graph does not have any spanning tree, as it cannot
be spanned to all its vertices.
21. We found three spanning trees off one complete graph. A complete
undirected graph can have maximum nn-2 number of spanning trees,
where n is the number of nodes. In the above addressed example, n
is 3, hence 33−2 = 3spanning trees are possible.
22. 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.
23. 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
Let us understand this through a small example. Consider, city
network as a huge graph and now plans to deploy telephone lines
in such a way that in minimum lines we can connect to all city
nodes. This is where the spanning tree comes into picture.
24. 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.
Minimum Spanning-Tree Algorithm
Kruskal's 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.
25. 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.