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)
Input : Adjacency list representation of graph shown on left side of below diagram Output: K-Cores :  -> 3 -> 4 -> 6  -> 2 -> 4 -> 6 -> 7  -> 2 -> 3 -> 6 -> 7  -> 2 -> 3 -> 4 -> 7  -> 3 -> 4 -> 6
We strongly recommend you to minimize your browser and try this yourself first.
The standard algorithm to find a k-core graph is to remove all the vertices that have degree less than- ‘K’ from the input graph. We must be careful that removing a vertex reduces the degree of all the vertices adjacent to it, hence the degree of adjacent vertices can also drop below-‘K’. And thus, we may have to remove those vertices also. This process may/may not go until there are no vertices left in the graph.
To implement above algorithm, we do a modified DFS on the input graph and delete all the vertices having degree less than ‘K’, then update degrees of all the adjacent vertices, and if their degree falls below ‘K’ we will delete them too.
Below is implementation of above idea. Note that the below program only prints vertices of k cores, but it can be easily extended to print the complete k cores as we have modified adjacency list.
K-Cores :  -> 3 -> 4 -> 6  -> 2 -> 4 -> 6 -> 7  -> 2 -> 3 -> 6 -> 7  -> 2 -> 3 -> 4 -> 7  -> 3 -> 4 -> 6 K-Cores :
Time complexity of the above solution is O(V + E) where V is number of vertices and E is number of edges.
Related Concepts :
Degeneracy : Degeneracy of a graph is the largest value k such that the graph has a k-core. For example, the above shown graph has a 3-Cores and doesn’t have 4 or higher cores. Therefore, above graph is 3-degenerate.
Degeneracy of a graph is used to measure how sparse graph is.
This article is contributed by Rachit Belwariar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find if there is a path between two vertices in an undirected graph
- Connected Components in an undirected graph
- Check if there is a cycle with odd weight sum in an undirected graph
- Cycles of length n in an undirected and connected graph
- Eulerian Path in undirected graph
- Number of single cycle components in an undirected graph
- Detect cycle in an undirected graph using BFS
- Maximum number of edges among all connected components of an undirected graph
- Sum of degrees of all nodes of a undirected graph
- Find a Mother Vertex in a Graph
- Find a Mother vertex in a Graph using Bit Masking
- Bridges in a graph
- Articulation Points (or Cut Vertices) in a Graph
- Biconnected graph
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Euler Circuit in a Directed Graph
- Assign directions to edges so that the directed graph remains acyclic
- Check if removing a given edge disconnects a graph
- Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
- Mathematics | Graph Isomorphisms and Connectivity