At each stage, include the least cost edge whose addition to the selected edges forms a tree.
The approach takes O(|E| log |V |) time for a graph G = (V, E). The time requirement is driven by the algorithm used for selecting the edges to be added in each stage.
Consequently, the algorithm takes O(|V |) + O(|V | log |V |) + O(|E| log |V |) = O(|E| log |V |) time.
A greedy algorithm: Visit the edges in order of increasing cost. Include just those edges that don’t create cycles.
Kruskal’s algorithm requires O(|E| log |E|) time for sorting the edges, O(|E|) time to traversing them, and O(|V | log |V |) time for checking the existence of cycles (employing the union-find algorithm below). Hence, the algorithm is of time complexity O(|E| log |E|).
Kruskal’s algorithm relies on a union-find algorithm for checking cycles. The “union” operation unites equivalent sets of elements, and the “find” operation determines the sets in which the elements reside.
A ![]() |
![]() |
H ![]() |
![]() |
A ![]() |
![]() |
D ![]() |
![]() |
E ![]() |
![]() |
E ![]() |
![]() |
D ![]() |
![]() |
C ![]() |
![]() |
Write a (real) program that reads the edges of a directed graph, internally stores the graph using the adjacency list approach, and then prints
A list of nodes is said to be topologically sorted, if node ‘A’ precedes node ‘B’ in the listing whenever there exists an edge from node ‘A’ to node ‘B’.
The lists of nodes ‘A,B,C’ and ‘A,C,B’ are the only topologically sorted lists for the the following graph. The list ‘B,A,C’ is an example of one which is not topologically ordered.
Hint: Assume an indegree field in each node.
Your program should be well documented, and accept files of pairs of integers for input. Each pair represents an edge.
Submit a printout of the program, of an input file, and of the corresponding output.