forked from anku580/Java-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathk_cores.java
More file actions
170 lines (143 loc) · 3.75 KB
/
k_cores.java
File metadata and controls
170 lines (143 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Java program to find K-Cores of a graph
import java.util.*;
class k_cores
{
// This class represents a undirected graph using adjacency
// list representation
static class Graph
{
int V; // No. of vertices
// Pointer to an array containing adjacency lists
Vector<Integer>[] adj;
@SuppressWarnings("unchecked")
Graph(int V)
{
this.V = V;
this.adj = new Vector[V];
for (int i = 0; i < V; i++)
adj[i] = new Vector<>();
}
// function to add an edge to graph
void addEdge(int u, int v)
{
this.adj[u].add(v);
this.adj[v].add(u);
}
// A recursive function to print DFS starting from v.
// It returns true if 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,
boolean DFSUtil(int v, boolean[] visited, 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
for (int i : adj[v])
{
// 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 printKCores(int k)
{
// INITIALIZATION
// Mark all the vertices as not visited and not
// processed.
boolean[] visited = new boolean[V];
boolean[] processed = new boolean[V];
Arrays.fill(visited, false);
Arrays.fill(processed, false);
int mindeg = Integer.MAX_VALUE;
int startvertex = 0;
// Store degrees of all vertices
int[] vDegree = new int[V];
for (int i = 0; i < V; i++)
{
vDegree[i] = adj[i].size();
if (vDegree[i] < mindeg)
{
mindeg = vDegree[i];
startvertex = i;
}
}
DFSUtil(startvertex, visited, vDegree, k);
// DFS traversal to update degrees of all
// vertices.
for (int i = 0; i < V; i++)
if (!visited[i])
DFSUtil(i, visited, vDegree, k);
// PRINTING K CORES
System.out.println("K-Cores : ");
for (int v = 0; v < V; v++)
{
// Only considering those vertices which have degree
// >= K after BFS
if (vDegree[v] >= k)
{
System.out.printf("\n[%d]", v);
// Traverse adjacency list of v and print only
// those adjacent which have vDegree >= k after
// BFS.
for (int i : adj[v])
if (vDegree[i] >= k)
System.out.printf(" -> %d", i);
}
}
}
}
// Driver Code
public static void main(String[] args)
{
// Create a graph given in the above diagram
int k = 3;
Graph g1 = new Graph(9);
g1.addEdge(0, 1);
g1.addEdge(0, 2);
g1.addEdge(1, 2);
g1.addEdge(1, 5);
g1.addEdge(2, 3);
g1.addEdge(2, 4);
g1.addEdge(2, 5);
g1.addEdge(2, 6);
g1.addEdge(3, 4);
g1.addEdge(3, 6);
g1.addEdge(3, 7);
g1.addEdge(4, 6);
g1.addEdge(4, 7);
g1.addEdge(5, 6);
g1.addEdge(5, 8);
g1.addEdge(6, 7);
g1.addEdge(6, 8);
g1.printKCores(k);
System.out.println();
Graph g2 = new Graph(13);
g2.addEdge(0, 1);
g2.addEdge(0, 2);
g2.addEdge(0, 3);
g2.addEdge(1, 4);
g2.addEdge(1, 5);
g2.addEdge(1, 6);
g2.addEdge(2, 7);
g2.addEdge(2, 8);
g2.addEdge(2, 9);
g2.addEdge(3, 10);
g2.addEdge(3, 11);
g2.addEdge(3, 12);
g2.printKCores(k);
}
}