Category Archives: Graph

Reverse Delete Algorithm for Minimum Spanning Tree

Reverse Delete algorithm is closely related to Kruskal’s algorithm. In Kruskal’s algorithm what we do is : Sort edges by increasing order of their weights. After sorting, we one by one pick edges in increasing order. We include current picked edge if by including this in spanning tree not form any cycle until there are… Read More »

Total number of Spanning Trees in a Graph

If a graph is a complete graph with n vertices, then total number of spanning trees is n(n-2) where n is the number of nodes in the graph. In complete graph, the task is equal to counting different labeled trees with n nodes for which have Cayley’s formula. What if graph is not complete? Follow… Read More »

Hierholzer’s Algorithm for directed graph

Given a directed Eulerian graph, print an Euler circuit. Euler circuit is a path that traverses every edge of a graph, and the path ends on the starting vertex. Examples: Input : Adjacency list for the below graph Output : 0 -> 1 -> 2 -> 0 Input : Adjacency list for the below graph… Read More »

Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)

We have introduced Graph basics in Graph and its representations. In this post, a different STL based representation is used that can be helpful to quickly implement graph using vectors. The implementation is for adjacency list representation of graph. Following is an example undirected and unweighted graph with 5 vertices. Below is adjacency list representation… Read More »

Check loop in array according to given constraints

Given an array arr[0..n-1] of positive and negative numbers we need to find if there is a cycle in array with given rules of movements. If a number at an i index is positive, then move arr[i]%n forward steps, i.e., next index to visit is (i + arr[i])%n. Conversely, if it’s negative, move backward arr[i]%n… Read More »

Minimum Product Spanning Tree

Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree and connects all the vertices together. A single graph can have many different spanning trees. A minimum product spanning tree for a weighted, connected and undirected graph is a spanning tree with weight product less than… Read More »