Find k-cores of an undirected graph - GeeksforGeeks
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
Read full article from Find k-cores of an undirected graph - GeeksforGeeks
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
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.
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.
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.
// A recursive function to print DFS starting from v.
// It returns true of degree of v after processing is less
// than k else false
// It also updates degree of adjacent if degree of v
// is less than k. And if degree of a processed adjacent
// becomes less than k, then it reduces of degree of v also,
bool
Graph::DFSUtil(
int
v, vector<
bool
> &visited,
vector<
int
> &vDegree,
int
k)
{
// Mark the current node as visited and print it
visited[v] =
true
;
// Recur for all the vertices adjacent to this vertex
list<
int
>::iterator i;
for
(i = adj[v].begin(); i != adj[v].end(); ++i)
{
// degree of v is less than k, then degree of adjacent
// must be reduced
if
(vDegree[v] < k)
vDegree[*i]--;
// If adjacent is not processed, process it
if
(!visited[*i])
{
// If degree of adjacent after processing becomes
// less than k, then reduce degree of v also.
if
(DFSUtil(*i, visited, vDegree, k))
vDegree[v]--;
}
}
// Return true if degree of v is less than k
return
(vDegree[v] < k);
}
// Prints k cores of an undirected graph
void
Graph::printKCores(
int
k)
{
// INITIALIZATION
// Mark all the vertices as not visited and not
// processed.
vector<
bool
> visited(V,
false
);
vector<
bool
> processed(V,
false
);
// Store degrees of all vertices
vector<
int
> vDegree(V);
for
(
int
i=0; i<V; i++)
vDegree[i] = adj[i].size();
// DFS traversal to update degrees of all
// vertices.
for
(
int
i=0; i<V; i++)
if
(visited[i] ==
false
)
DFSUtil(i, visited, vDegree, k);
// PRINTING K CORES
cout <<
"K-Cores : \n"
;
for
(
int
v=0; v<V; v++)
{
// Only considering those vertices which have degree
// >= K after BFS
if
(vDegree[v] >= k)
{
cout <<
"\n["
<< v <<
"]"
;
// Traverse adjacency list of v and print only
// those adjacent which have vDegree >= k after
// BFS.
list<
int
>::iterator itr;
for
(itr = adj[v].begin(); itr != adj[v].end(); ++itr)
if
(vDegree[*itr] >= k)
cout <<
" -> "
<< *itr;
}
}
}
Read full article from Find k-cores of an undirected graph - GeeksforGeeks