'Graph exercise transitive closure
i have to find transitive closure of a given graph. So the method which craetes transitive closure should be working like this: G = ABCD --> G+ = AB AC AD BC BD CD , G is a given graph and G+ is a transitive closure. So far my method outputs AB AC AD from ABCD, but how to output the rest BC BD CD? i´ve created :
Graph class:
class Graph {
String id;
Vertex first;
Graph (String s, Vertex v) {
id = s;
first = v;
}
Graph (String s) {
this (s, null);
}
public LinkedList<Edge> transitiveClosure(LinkedList<Vertex> v)
{
Graph g = new Graph("G+");
LinkedList<Edge> edge = new LinkedList<Edge>();
Edge e = null;
Iterator i = v.iterator();
for(Vertex vertex : v)
{
System.out.print(vertex);
while(i.hasNext()) {
g.first = v.getFirst();
Vertex tmp = v.getFirst();
tmp =(Vertex) i.next();
if(tmp == g.first)
tmp = (Vertex)i.next();
e = new Edge(g.first.toString() + tmp);
edge.add(e);
//v.remove(g.first);
}
}
System.out.println();
return edge;
}
Vertex class
class Vertex {
String id;
Vertex next;
Edge first;
Vertex (String s, Vertex v, Edge e) {
id = s;
next = v;
first = e;
}
Vertex (String s) {
this (s, null, null);
}
@Override
public String toString() {
return id;
}
// TODO!!! Your Vertex methods here!
}
Edge class
class Edge {
String id;
Vertex target;
Edge next;
Edge (String s, Vertex v, Edge e) {
id = s;
target = v;
next = e;
}
Edge (String s) {
this (s, null, null);
}
@Override
public String toString() {
return id;
}
// TODO!!! Your Edge methods here!
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|