I need to write a graph with some nodes v={1,2,3,4,5,6,7} and edges E={(1-2),(2-3), (3,4),(4-7), (3-6), (2-6), (5-6), (1-5)} and also with some demands D=(s,d)={(1-3),(2-3), (4-5). (7-7), (6-4),(2-7)} which are some path with an exact source an destination for a small network. How can I write this in Java? Can anyone help me please?
Asked
Active
Viewed 1,368 times
3 Answers
1
Extracting out the nouns you will see that you need a Vertices/Node class, an Edge class and a Demand class. This will be just the beginning of your solution. You will also have a Graph or Network class that consists of a list of Nodes, a list of Edges and a list of Demands. Then you will need to decide where to put the processing methods that use the nodes, edges and demands.
> A Node will consist of an integer.
> An Edge will consist of two Nodes
> and so on
Here is an example
public class Node {
private int nodeNumber;
}

Sachin Kainth
- 45,256
- 81
- 201
- 304
-
thx , but since i am new to java, can you help me more in details i mean with some java codes for classes – user2560937 Jul 15 '13 at 13:06
0
If you don't want to reinvent the weel, you could look into JGraphT. There are some examples available that give you some ideas of how you can use it.

mthmulders
- 9,483
- 4
- 37
- 54
0
As a start point I can propose you following class structure:
class Vertex{
String id;
List<Edge> edges;
public Vertex(String id){
this.id = id;
edges = new ArrayList<Edge>();
}
void addEdge(Edge e){
edges.add(e);
}
}
class Edge{
Vertex fromVertex;
Vertex toVertex;
Edge(Vertex from, Vertex to){
this.fromVertex = from;
this.toVertex = to;
}
}
etc...
If you consider not to implement your own graph and use it from some library take a look at this question: