-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphs3.java
More file actions
174 lines (140 loc) · 4.73 KB
/
Copy pathGraphs3.java
File metadata and controls
174 lines (140 loc) · 4.73 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
170
171
172
173
174
import java.util.*;
import javax.imageio.ImageIO;
import org.w3c.dom.html.HTMLDivElement;
public class Graphs3 {
static class Edge implements Comparable<Edge>{
int src;
int dest;
int wt;
public Edge(int s , int d , int w){
this.src = s;
this.dest = d;
this.wt = w;
}
@Override
public int compareTo(Edge e2){
return this.wt - e2.wt;
}
}
// public static int connectCities(int cities[][]){
// PriorityQueue<Edge> pq = new PriorityQueue<>();
// boolean vis[] = new boolean[cities.length];
// pq.add(new Edge(0, 0));
// int finalCost = 0;
// while(!pq.isEmpty()){
// Edge curr = pq.remove();
// if(!vis[curr.dest]){
// vis[curr.dest] = true;
// finalCost += curr.cost;
// for(int i = 0; i< cities[curr.dest].length; i++){
// if(cities[curr.dest][i] != 0){
// pq.add(new Edge(i, cities[curr.dest][i]));
// }
// }
// }
// }
// return finalCost;
// }
static void createGraph(ArrayList<Edge> edges){
edges.add(new Edge(0, 1, 10));
edges.add(new Edge(0, 2, 15));
edges.add(new Edge(0, 3, 30));
edges.add(new Edge(1, 3, 40));
edges.add(new Edge(2, 3, 50));
}
static int n = 4; // In case of krshkal algo the value of n is vertices
static int par[] = new int[n];
static int rank[] = new int[n];
public static void init(){
for(int i = 0; i< n; i++){
par[i] = i;
}
}
public static int find(int x){
if(par[x] == x){
return x;
}
return find(par[x]);
}
public static void union(int a , int b){
int parA = find(a);
int parB = find(b);
if(rank[parA] == rank[parB]){
par[parB] = parA;
rank[parA]++;
}else if(rank[parA] < rank[parB]){
par[parA] = parB;
}else{
par[parB] = parA;
}
}
public static void krushkalsMST(ArrayList<Edge> edges , int V){
init();
Collections.sort(edges);
int mstCost = 0;
int count = 0;
for(int i = 0; count < V - 1; i++){
Edge e = edges.get(i);
int parA = find(e.src);
int parB = find(e.dest);
if(parA != parB){
union(e.src, e.dest);
mstCost += e.wt;
count++;
}
}
System.out.println(mstCost);
}
public static void helper(int image[][] , int sr , int sc , int color , boolean vis[][] , int orgCol){
if(sr < 0 || sc < 0 || sr >= image.length || sc >= image[0].length || vis[sr][sc] || image[sr][sc] != orgCol){
return;
}
image[sr][sc] = color;
// up
helper(image, sr - 1 , sc, color, vis, orgCol);
// down
helper(image, sr + 1 , sc, color, vis, orgCol);
// right
helper(image, sr, sc + 1, color, vis, orgCol);
// left
helper(image, sr, sc - 1, color, vis, orgCol);
}
public static void floodFill(int image[][] , int sr , int sc , int color){
boolean vis[][] = new boolean[image.length][image[0].length];
helper(image , sr , sc , color , vis , image[sr][sc]);
for(int i = 0; i< image.length; i++){
for(int j = 0; j < image[0].length; j++){
System.out.print(image[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
// int cities[][] = {{0,1,2,3,4},
// {1,0,5,0,7},
// {2,5,0,6,0},
// {3,0,6,0,0},
// {4,7,0,0,0}};
// System.out.println(connectCities(cities));
// init();
// union(1, 3);
// System.out.println(find(3));
// union(2, 4);
// union(3, 6);
// union(1, 4);
// System.out.println(find(3));
// System.out.println(find(4));
// union(1, 5);
// int V = 4;
// ArrayList<Edge> edges = new ArrayList<>();
// createGraph(edges);
// krushkalsMST(edges, V);
int image[][] = {{1,1,1},
{1,1,0},
{1,0,1}};
int sr = 1;
int sc = 1;
int color = 2;
floodFill(image, sr, sc, color);
}
}