The example creates an empty graph and uses readGraph() with the file samplegraph.dat to load the vertices, edges and weights. An output statement uses toString() to display the graph. The following is a listing of the file with an accompanying picture of the graph :
- File samplegraph.dat :
Here we create a class JGraph which implements Graph interface to carry out the example here :
Output :
- File samplegraph.dat :
5 // data for the vertices
A B C D E
6 // data for the edges
A B 3
A C 2
B C 6
C B 4
C D 1
E B 5
Here we create a class JGraph which implements Graph interface to carry out the example here :
- JGraph.java :
- package DSwJ.S24;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Set;
- import java.util.TreeMap;
- import DSwJ.S24.sub.Edge;
- public class JGraph
implements Graph{ - private TreeMap
>> vertexTable; - public JGraph(){vertexTable = new TreeMap
>>();} - public static JGraph readGraph(String fn) {
- JGraph
graph = new JGraph (); - File graphFile = new File(fn);
- int vertexCnt = 0;
- int edgeCnt = 0;
- if(graphFile.exists()) {
- try{
- BufferedReader br = new BufferedReader(new FileReader(graphFile));
- String line;
- if((line=br.readLine())!=null) { //Read vertex count
- vertexCnt = Integer.valueOf(line.trim());
- //System.out.println("Vertex Cnt="+vertexCnt+"...");
- } else return null;
- if((line=br.readLine())!=null) { //Read vertex set
- String[] vertexSet = line.split(" ");
- vertexCnt = vertexCnt>vertexSet.length?vertexSet.length:vertexCnt;
- for(int i=0; i
- //System.out.println("Add Vertex("+vertexSet[i]+")");
- graph.addVertex(vertexSet[i].trim());
- }
- } else return null;
- if((line=br.readLine())!=null) { //Read edge count
- edgeCnt = Integer.valueOf(line);
- } else return null;
- while((line=br.readLine())!=null && edgeCnt>0) {
- String[] edgeSet = line.split(" ");
- if(edgeSet.length==3) {
- graph.addEdge(edgeSet[0].trim(), edgeSet[1].trim(), Integer.valueOf(edgeSet[2]));
- }
- edgeCnt--;
- }
- br.close();
- }catch(Exception e){e.printStackTrace(); return null;}
- }
- return graph;
- }
- @Override
- public boolean addEdge(Object v1, Object v2, int w) {
- if(vertexTable.containsKey(v1) && vertexTable.containsKey(v2)) {
- return vertexTable.get(v1).add(new Edge
((T)v1, (T)v2, w)); - }
- throw new java.lang.IllegalArgumentException("JGraph(addEdge): Vertices "+v1+" or "+v2+" doesn't exist");
- }
- @Override
- public boolean addVertex(Object v) {
- if(!vertexTable.containsKey(v)){
- vertexTable.put((T)v, new ArrayList
>()); - return true;
- }
- return false;
- }
- @Override
- public void clear() {
- vertexTable.clear();
- }
- @Override
- public boolean containsEdge(Object v1, Object v2) {
- if(vertexTable.containsKey(v1) && vertexTable.containsKey(v2)) {
- ArrayList
> list = vertexTable.get(v1); - if(list!=null) {
- Iterator
> iter = list.iterator(); - while(iter.hasNext()) {
- Edge
e = iter.next(); - if(e.dest.equals(v2)) return true;
- }
- }
- return false;
- }
- throw new java.lang.IllegalArgumentException("JGraph(containsEdge): Vertices "+v1+" or "+v2+" doesn't exist");
- }
- @Override
- public boolean containsVertex(Object v) {
- return vertexTable.containsKey(v);
- }
- @Override
- public Set getNeighbors(Object v) {
- if(vertexTable.containsKey(v)) {
- HashSet
neighborSet = new HashSet (); - return neighborSet;
- }
- throw new java.lang.IllegalArgumentException("JGraph(containsEdge): Vertex "+v+" doesn't exist");
- }
- @Override
- public int getWeight(Object v1, Object v2) {
- if(vertexTable.containsKey(v1) && vertexTable.containsKey(v2)) {
- List
> list = vertexTable.get(v1); - if(list!=null) {
- Iterator
> iter = list.iterator(); - while(iter.hasNext()) {
- Edge
e = iter.next(); - if(e.dest.equals(v2)) return e.weight;
- }
- }
- return -1;
- }
- throw new java.lang.IllegalArgumentException("JGraph(getWeight): Vertices "+v1+" or "+v2+" doesn't exist");
- }
- @Override
- public boolean isEmpty() {
- return vertexTable.isEmpty();
- }
- @Override
- public int numberOfEdges() {
- int total=0;
- Set
keySet = vertexTable.keySet(); - Iterator
iter = keySet.iterator(); - while(iter.hasNext()) {
- total+=vertexTable.get(iter.next()).size();
- }
- return total;
- }
- @Override
- public int numberOfVertices() {
- return vertexTable.size();
- }
- @Override
- public boolean removeEdge(Object v1, Object v2) {
- if(vertexTable.containsKey(v1) && vertexTable.containsKey(v2)) {
- List
> list = vertexTable.get(v1); - if(list!=null) {
- Iterator
> iter = list.iterator(); - while(iter.hasNext()) {
- Edge
e = iter.next(); - if(e.dest.equals(v2)){
- return list.remove(e);
- }
- }
- }
- return false;
- }
- throw new java.lang.IllegalArgumentException("JGraph(removeEdge): Vertices "+v1+" or "+v2+" doesn't exist");
- }
- @Override
- public boolean removeVertex(Object v) {
- if(vertexTable.remove(v)!=null) return true;
- return false;
- }
- @Override
- public int setWeight(Object v1, Object v2, int w) {
- if(vertexTable.containsKey(v1) && vertexTable.containsKey(v2)) {
- List
> list = vertexTable.get(v1); - if(list!=null) {
- Iterator
> iter = list.iterator(); - while(iter.hasNext()) {
- Edge
e = iter.next(); - if(e.dest.equals(v2)){
- int prevW = e.weight;
- e.weight = w;
- return prevW;
- }
- }
- }
- return -1;
- }
- throw new java.lang.IllegalArgumentException("JGraph(setWeight): Vertices "+v1+" or "+v2+" doesn't exist");
- }
- @Override
- public Set vertexSet() {
- return vertexTable.keySet();
- }
- public int outDegree(T vertex) {
- if(vertexTable.containsKey(vertex)) {
- return vertexTable.get(vertex).size();
- }
- throw new java.lang.IllegalArgumentException("JGraph(outDegree): Vertex "+vertex+" doesn't exist");
- }
- public int inDegree(T vertex){
- int cnt = 0;
- if(vertexTable.containsKey(vertex)) {
- Set
keySet = vertexTable.keySet(); - Iterator
iter = keySet.iterator(); - while(iter.hasNext()) {
- T v = iter.next();
- if(!v.equals(vertex)) {
- List
> edges = vertexTable.get(v); - Iterator
> eIter = edges.iterator(); - while(eIter.hasNext()) if(eIter.next().dest.equals(vertex)) cnt++;
- }
- }
- return cnt;
- }
- throw new java.lang.IllegalArgumentException("JGraph(inDegree): Vertex "+vertex+" doesn't exist");
- }
- public int size(){return vertexTable.size();}
- @Override
- public String toString(){
- StringBuffer buffer = new StringBuffer("");
- Set
keySet = vertexTable.keySet(); - Iterator
iter = keySet.iterator(); - while(iter.hasNext()) {
- T vertex = iter.next();
- buffer.append(vertex+":\tin-degree "+inDegree(vertex)+" out-degree "+outDegree(vertex)+"\n");
- buffer.append("\tEdges: ");
- List
> edges = vertexTable.get(vertex); - Iterator
> eIter = edges.iterator(); - while(eIter.hasNext()) {
- Edge
edge = eIter.next(); - buffer.append(edge.dest+"("+edge.weight+") ");
- }
- buffer.append("\n");
- }
- return buffer.toString();
- }
- public static void main(String args[]) {
- JGraph
graph = JGraph.readGraph("samplegraph.dat"); - System.out.println(graph);
- }
- }
Output :
A: in-degree 0 out-degree 2
Edges: B(3) C(2)
B: in-degree 3 out-degree 1
Edges: C(6)
C: in-degree 2 out-degree 2
Edges: B(4) D(1)
D: in-degree 1 out-degree 0
Edges:
E: in-degree 0 out-degree 1
Edges: B(5)
This message was edited 3 times. Last update was at 08/11/2010 16:20:12
沒有留言:
張貼留言