// Use this for initialization void Start() { //测试图的基本功能 // testGraphBasicOperation (); //从File读取图 print("*********测试稠密图的读取文件获得数据*********"); string filename1 = "testG1.txt"; string filename2 = "testG2.txt"; string url = FileHelper.FileNameHelper(filename1); bool isDirect = false; DenseGraph dGraph = null; ReadGraph.ReadGraphFromFile(url, out dGraph, isDirect); dGraph.print(); dGraph = null; ReadGraph.ReadGraphFromFile(url, out dGraph, isDirect); url = FileHelper.FileNameHelper(filename2); ReadGraph.ReadGraphFromFile(url, out dGraph, isDirect); dGraph.print(); print("*******************************************"); print("*********测试稀疏图的读取文件获得数据*********"); url = FileHelper.FileNameHelper(filename1); SpareGraph sGraph = null; ReadGraph.ReadGraphFromFile(url, out sGraph, isDirect); sGraph.print(); sGraph = null; ReadGraph.ReadGraphFromFile(url, out sGraph, isDirect); url = FileHelper.FileNameHelper(filename2); ReadGraph.ReadGraphFromFile(url, out sGraph, isDirect); sGraph.print(); }
// Use this for initialization void Start() { string filename = "testG1.txt"; SpareGraph sGraph = null; string url = FileHelper.FileNameHelper(filename); ReadGraph.ReadGraphFromFile(url, out sGraph, false); sGraph.print(); Components SpareGraphcomponent = new Components(sGraph); SpareGraphcomponent.MinLength1(0); SpareGraphcomponent.MinLength(0); }
//使用广度或者深度搜索遍历稀疏图 public Components(SpareGraph sGraph) { this.sgraph = sGraph; Debug.Log("V=" + sGraph.V() + " E = " + sGraph.E()); this.n = sGraph.V(); this.m = sGraph.E(); count = 0; id = new int[n]; book = new bool[n]; from = new int[n]; for (int i = 0; i != n; i++) { from [i] = -1; book [i] = false; id [i] = -1; } //Spare DSF for (int v = 0; v != n; v++) { if (!book[v]) { count++; id [v] = count; book [v] = true; from [v] = -1; SpareGraphDFS(v); } } Debug.Log("图里有几个联通分量 : " + count); for (int i = 0; i != n; i++) { findPath(i); } // //Spare BSF // for (int v = 0; v != n; v++) { // if (!book [v]) { // count++; // id [v] = count; // book [v] = true; // from [v] = -1; // SpareGraphBSF (v); // } // } // Debug.Log ("图里有几个联通分量 : " + count); // for (int v = 0; v != n; v++) { // findPath (v); // } }
public void testNormalComponent() { print("******测试稠密图的深度优先搜索******"); DenseGraph dGraph = null; string filename = "testG1.txt"; string url = FileHelper.FileNameHelper(filename); ReadGraph.ReadGraphFromFile(url, out dGraph, false); Components DenseGraphcomponent = new Components(dGraph); print("******测试稀疏图的深度优先搜索******"); SpareGraph sGraph = null; url = FileHelper.FileNameHelper(filename); ReadGraph.ReadGraphFromFile(url, out sGraph, false); sGraph.print(); Components SpareGraphcomponent = new Components(sGraph); }
public static void ReadGraphFromFile(string fileName, out SpareGraph graph, bool isDirected) { int V, E; string[] strs = File.ReadAllLines(fileName); Debug.Assert(strs.Length >= 2); string veline = strs [0]; readGraphFileVELine(veline, out V, out E); // Debug.Log ("V , E " + V +"," + E); graph = new SpareGraph(V, isDirected); for (int i = 0; i != E; i++) { string line = strs [i + 1]; int p, q; readGraphFileVELine(line, out p, out q); graph.AddEdge(p, q); } }
public void testGraphBasicOperation() { //*********测试稠密图的基本功能********* print("*********测试稠密图的基本功能*********"); int n = 10; int m = 3; bool isDirected = false; //无向图 DenseGraph dGraph = new DenseGraph(n, isDirected); for (int i = 0; i != m; i++) { int p = Random.Range(0, n); int q = Random.Range(0, n); dGraph.AddEdge(p, q); } dGraph.print(); print("* *"); string rowsStr = ""; for (int i = 0; i != n; i++) { DenseGraph.adjIterator iter = new DenseGraph.adjIterator(dGraph, i); for (int hasEdge = iter.begin(); !iter.IsEnd(); hasEdge = iter.next()) { rowsStr += hasEdge + " "; } rowsStr += "\n"; } print(rowsStr); print("*************************************"); print("*********测试稀疏图图的基本功能*********"); n = 10; m = 3; isDirected = false; //无向图 SpareGraph sGraph = new SpareGraph(n, isDirected); dGraph = new DenseGraph(n, isDirected); for (int i = 0; i != m; i++) { int p = Random.Range(0, n); int q = Random.Range(0, n); sGraph.AddEdge(p, q); dGraph.AddEdge(p, q); } // dGraph.print (); print("* *"); rowsStr = ""; for (int i = 0; i != n; i++) { SpareGraph.adjIterator iter = new SpareGraph.adjIterator(sGraph, i); rowsStr += "第" + i + "结点 "; for (int hasEdge = iter.begin(); !iter.isEnd(); hasEdge = iter.Next()) { rowsStr += hasEdge + " "; } rowsStr += "\n"; } print(rowsStr); dGraph.print(); print("*************************************"); }
public adjIterator(SpareGraph graph, int v) { this.graph = graph; this.v = v; index = 0; }