Skip to content
Snippets Groups Projects
Commit 8a7810f2 authored by Lawrence Chung's avatar Lawrence Chung
Browse files

make CC, edit Fish iterator

parent e7409ea8
No related branches found
No related tags found
No related merge requests found
package search;
public class CC {
private boolean[] marked;
private int[] id;
private int count;
public static void main(String[] args) {
Graph g = new Graph(5);
g.addEdge(4, 3);
g.addEdge(2, 3);
g.addEdge(1, 2);
g.addEdge(0, 2);
CC component = new CC(g);
System.out.println(component.connected(0, 2));
}
public CC(Graph G){
marked = new boolean[G.V()];
id = new int[G.V()];
for(int s = 0; s < G.V(); s++){
if(!marked[s]){
dfs(G,s);
count++;
}
}
}
private void dfs(Graph G, int v){
marked[v] = true;
id[v] = count;
for(int w : G.adj(v)){
if(!marked[w])
dfs(G, w);
}
}
public boolean connected(int v, int w){
return id[v] == id[w];
}
public int id(int v){
return id[v];
}
public int count(){
return count;
}
}
......@@ -20,8 +20,23 @@ public class Fish<Integer> implements Iterable<Integer> {
@Override
public Iterator<Integer> iterator() {
// TODO Auto-generated method stub
return null;
return new ListIterator();
}
private class ListIterator implements Iterator<Integer>{
private Node current = first;
public boolean hasNext(){
return current != null;
}
public Integer next(){
Integer item = current.item;
current = current.next;
return item;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment