2011-11-20 69 views
2

我想使用Jgraph創建圖形,然後使用該圖形查找最小生成樹。使用Jgraph創建圖表

如何使用Jgraph?創建圖形。


這就是我所實施的。你能告訴我如何使用包中的kruskals算法。我GOOGLE了它,但無法找到任何信息。

import org.jgrapht.*; 
import org.jgrapht.graph.*; 

public class MyGraph { 
    UndirectedGraph<String, DefaultEdge> g = new SimpleGraph<String, DefaultEdge>  (DefaultEdge.class); 

    public void addVertex(String name) { 
     // name=new String(); 
     g.addVertex(name); 
    } 

    public void addEdge(String v1,String v2) { 
     g.addEdge(v1, v2); 
    } 

    public UndirectedGraph<String, DefaultEdge> getGraph() { 
     return g; 
    } 
} 
+0

whats jgraph?你能提供一個鏈接嗎?你讀過文檔/示例代碼嗎? – yurib

+0

http://www.jgrapht.org/ – nyfer

回答

2

這是用戶輸入的主要類,例如沒有邊和頂點用於創建圖形的生成樹的圖形。以下是這個問題的完整答案。

import java.util.Scanner; 

public class Main { 

    public static void main(String args[]) { 

     int x; 
     Scanner sc=new Scanner(System.in); 
     MyGraph my=new MyGraph(); 
     System.out.println("Enter the no of vertices"); 
     int no_of_ver=sc.nextInt(); 

     for(int i=1;i<=no_of_ver;i++) { 
      System.out.println("Enter vertex"+i); 
      my.addVertex(sc.next()); 
     } 

     do { 
      System.out.println("Enter the edges"); 
      String e1=sc.next(); 
      String e2=sc.next(); 
      my.addEdge(e1, e2); 
      // my.setEdgeWeight(); 
      System.out.println("Continue... Yes:1 ********** No:0"); 
      x=sc.nextInt(); 
     } while(x==1); 

     System.out.println("Graph\n"+my.getGraph().toString()); 
     System.out.println("\n\n**********Spanning Tree*********"); 
     my.getSpanningTree(); 
     // System.out.println("\nSpanning Tree Cost"); 
     //my.getSpanningTreeCost(); 
    } 
} 

下面的MyGraph類完成了通過邊和計算生成樹來創建圖的所有工作。我用jgrapht庫創建圖表

import org.jgrapht.*; 
import org.jgrapht.graph.*; 
import org.jgrapht.alg.KruskalMinimumSpanningTree; 

public class MyGraph { 

    private UndirectedGraph<String, DefaultEdge> g = new SimpleGraph<String, DefaultEdge>(DefaultEdge.class); 
    static final double DEFAULT_EDGE_WEIGHT=19; 
    DefaultWeightedEdge>(DefaultWeightedEdge.class); 
    private DefaultWeightedEdge e1; 

    public void addVertex(String name) { 
     g.addVertex(name); 
     //graph.addVertex(name); 
    } 

    public void addEdge(String v1,String v2) { 
     g.addEdge(v1, v2); 
     // e1=graph.addEdge(v1, v2); 
    } 

    /*public void setEdgeWeight() { 
     graph.setEdgeWeight(e1, DEFAULT_EDGE_WEIGHT);   
    }*/ 

    public UndirectedGraph<String, DefaultEdge> getGraph() { 
     return g; 
    } 

    /*public SimpleWeightedGraph<String,DefaultWeightedEdge> getGraph() { 
     return graph; 
    }*/ 

    public void getSpanningTree() { 
     KruskalMinimumSpanningTree k=new KruskalMinimumSpanningTree(g); 
     System.out.println(k.getEdgeSet().toString()); 
     //KruskalMinimumSpanningTree k1=new KruskalMinimumSpanningTree(graph); 
     //System.out.println(k1.getEdgeSet().toString()); 
    } 

    /*public void getSpanningTreeCost() { 
     KruskalMinimumSpanningTree k=new KruskalMinimumSpanningTree(graph); 
     System.out.println(k.getSpanningTreeCost()); 
    }*/ 
} 
+0

我嘗試使用jGraphT庫...我在日誌中獲得邊緣和頂點細節,但沒有顯示在UI上。任何想法我失蹤? – seema