Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ccdeb3c

Browse files
Added Code
1 parent e56ed5e commit ccdeb3c

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

‎35. Graph 04 - Directed Graph Problems/Kosaraju_Algo.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ public void dfs_helper(int node, int[] visited, Stack<Integer> st) {
6868
for (int c : this.graph.get(node)) {
6969
if (visited[c] == 0) {
7070
dfs_helper(c, visited, st);
71-
st.add(c);
71+
7272
}
7373
}
74+
st.add(node);
7475
}
7576

7677
}

‎36. Graph 05 - Disjoint Set Union/Disjoint_Set_Union.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public int get_superparent(int x) {
2323

2424
if(this.parent[x]==x)
2525
return x;
26-
26+
// Path Compression
2727
else return parent[x] = get_superparent(parent[x]);
2828
}
2929

@@ -33,6 +33,7 @@ public void union(int x, int y) {
3333
if(superparent_x != superparent_y) {
3434
this.parent[superparent_x] = superparent_y;
3535
this.noOfComponents-=1;
36+
// x is connected to y, so size of SP of x = 0 and size of SP y += size(SP x)
3637
this.size[superparent_y] += this.size[superparent_x];
3738
this.size[superparent_x] = 0;
3839
}
@@ -68,7 +69,9 @@ public static void main(String[] args) {
6869

6970
int ans = 0;
7071
for(int i=0;i<N; i++) {
71-
int superparent_i = g.get_superparent(i);
72+
int superparent_i = g.get_superparent(i);
73+
// g.size[superparent_i] gives size of that connected comp, so n- g.size[superparent_i] will give no of nodes
74+
// which are in diff comp.
7275
ans+=(N-g.size[superparent_i]);
7376
}
7477
//g.display_arr(g.size);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /