Category Archives: Graph

Push Relabel Algorithm | Set 2 (Implementation)

We strongly recommend to refer below article before moving on to this article. Push Relabel Algorithm | Set 1 (Introduction and Illustration) Problem Statement : Given a graph which represents a flow network where every edge has a capacity. Also given two vertices source ‘s’ and sink ‘t’ in the graph, find the maximum possible… Read More »

Kahn’s algorithm for Topological Sorting

Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge uv, vertex u comes before v in the ordering. Topological Sorting for a graph is not possible if the graph is not a DAG. For example, a topological sorting of the following graph is “5 4… Read More »

Push Relabel Algorithm | Set 1 (Introduction and Illustration)

Given a graph which represents a flow network where every edge has a capacity. Also given two vertices source ‘s’ and sink ‘t’ in the graph, find the maximum possible flow from s to t with following constraints: a) Flow on an edge doesn’t exceed the given capacity of the edge. b) Incoming flow is… Read More »

Find k-cores of an undirected graph

Given a graph G and an integer K, K-cores of the graph are connected components that are left after all vertices of degree less than k have been removed (Source wiki) Example: Input : Adjacency list representation of graph shown on left side of below diagram Output: K-Cores : [2] -> 3 -> 4 ->… Read More »

Prim’s algorithm using priority_queue in STL

Given an undirected, connected and weighted graph, find Minimum Spanning Tree (MST) of the graph using Prim’s algorithm. Input : Adjacency List representation of above graph Output : Edges in MST 0 – 1 1 – 2 2 – 3 3 – 4 2 – 5 5 – 6 6 – 7 2 – 8… Read More »