Graph
Graph
Components of a Graph
Breadth-First Search
Start putting anyone vertices from the graph at the back of the
queue.
First, move the front queue item and add it to the list of the
visited node.
Next, create nodes of the adjacent vertex of that list and add
them which have not been visited yet.
Keep repeating steps two and three until the queue is found to
be empty.
Pseudocode
Applications
Depth-first search
Algorithm
Pseudocode
Step 2: Calculate the work done or cost at each level and count total no of
levels in recursion tree
Algorithm:
Step 1: Determine an arbitrary vertex as the starting vertex of the MST.
Step 2: Follow steps 3 to 5 till there are vertices that are not included in
the MST (known as fringe vertex).
Step 3: Find edges connecting any tree vertex with the fringe vertices.
Step4: Find the minimum among these edges.
Step 5: Add the chosen edge to the MST if it does not form any cycle.
Step 6: Return the MST and exit
Step 1 - First, we have to choose a vertex from the above graph. Let's
choose B.
Step 2 - Now, we have to choose and add the shortest edge from
vertex B. There are two edges from vertex B that are B to C with
weight 10 and edge B to D with weight 4. Among the edges, the edge
BD has the minimum weight. So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight
among all the other edges. In this case, the edges DE and CD are such
edges. Add them to MST and explore the adjacent of C, i.e., E and A.
So, select the edge DE and add it to the MST.
Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge
CE as it would create a cycle to the graph. So, choose the edge CA
and add it to the MST.
So, the graph produced in step 5 is the minimum spanning tree of the
given graph. The cost of the MST is given below -
Kruskal's Algorithm
has the minimum sum of weights among all the trees that can be
formed from the graph
Choose the edge with the least weight, if there are more than 1, choose
anyone
Choose the next shortest edge that doesn't create a cycle and add it