Ejemplo n.º 1
0
        public void Test()
        {
            var weights = new DenseGraph <int?>(new int?[, ]
            {
                { 0000, 0007, 0009, null, null, 0014 },
                { 0007, 0000, 0010, 0014, null, null },
                { 0009, 0010, 0000, 0011, null, 0002 },
                { null, 0014, 0011, 0000, 0006, null },
                { null, null, null, 0006, 0000, 0009 },
                { 0014, null, 0002, null, 0009, 0000 },
            });
            var floydWarshall       = new FloydWarshall(weights);
            var floydWarshallResult = floydWarshall.GetResult();
            var dijkstra            = new Dijkstra(weights);

            for (var i = 0; i < weights.VertexCount; i++)
            {
                var dijkstraResult = dijkstra.GetResult(i);
                Assert.Equal(floydWarshallResult.distance.SliceRow(i), dijkstraResult.Select(x => x.Distance));
                for (var j = 0; j < weights.VertexCount; j++)
                {
                    Assert.Equal(floydWarshall.GetPath(floydWarshallResult.next, i, j), dijkstra.GetPath(dijkstraResult, i, j));
                }
            }
        }
Ejemplo n.º 2
0
    // 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();
    }
Ejemplo n.º 3
0
        public static void TestIterator()
        {
            int N  = 20;
            int M  = 100;
            var g1 = GenerateGraph(N, M);

            for (var v = 0; v < M; v++)
            {
                Console.Write(v + " : ");
                var adj = new DenseGraph <double> .AdjIterator(g1, v);

                for (var w = adj.Begin(); !adj.End(); w = adj.Next())
                {
                    Console.Write(w + " ");
                }
                Console.WriteLine("");
            }

            for (var v = 0; v < M; v++)
            {
                Console.Write(v + " : ");
                var adj = new DenseGraph <double> .AdjIterator(g1, v);

                foreach (int g in adj)
                {
                    Console.Write(g + " ");
                }
                Console.WriteLine("");
            }
        }
Ejemplo n.º 4
0
        public void ReadGraphTest()
        {
            string fileName = @"Graph\testG1.txt";

            var g1  = new SparseGraph <double>(13, false);
            var rg1 = new ReadGraph <double>(g1, fileName);

            g1.Show();

            var g2  = new DenseGraph <double>(13, false);
            var rg2 = new ReadGraph <double>(g2, fileName);

            g2.Show();
        }
Ejemplo n.º 5
0
        public static DenseGraph <double> GenerateGraph(int N, int M)
        {
            var random = new Random();
            var g1     = new DenseGraph <double>(N, false);

            //generate edges
            for (var i = 0; i < M; i++)
            {
                int a = random.Next() % N;
                int b = random.Next() % N;
                g1.AddEdge(a, b, 0);
            }

            return(g1);
        }
Ejemplo n.º 6
0
    //使用广度搜索或者深度搜索稠密图,包含路径的寻找
    public Components(DenseGraph graph)
    {
        dgraph = graph;
        count  = 0;
        book   = new bool[dgraph.V()];
        id     = new int[dgraph.V()];
        from   = new int[dgraph.V()];
        for (int i = 0; i != dgraph.V(); i++)
        {
            id [i]   = -1;
            book [i] = false;
            from [i] = -1;
        }

//		//dfs
//		for (int i = 0; i != dgraph.V (); i++) {
//			if (!book [i]) {
//				book[i] = true;
//				from [i] = -1;
//				DenseGraphDFS (i);
//				count++;
//
//			}
//		}
//		for (int i = 0; i != dgraph.V (); i++) {
//			findPath (i);
//		}
//		Debug.Log ("图里有几个联通分量 : " + count);
        //广度优先搜索BSF
        for (int i = 0; i != dgraph.V(); i++)
        {
            if (!book [i])
            {
                DenseGraphBSF(i);
                book [i] = true;
                from [i] = -1;
                count++;
            }
        }
        Debug.Log("图里有几个联通分量 : " + count);

//		for (int i = 0; i != dgraph.V (); i++) {
//			findPath (i);
//		}
    }
Ejemplo n.º 7
0
    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);
    }
Ejemplo n.º 8
0
    public static void ReadGraphFromFile(string fileName, out DenseGraph 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 DenseGraph(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);
        }
    }
Ejemplo n.º 9
0
        public void DenseGraph()
        {
            var weights = new DenseGraph <int?>(new int?[, ]
            {
                { 0000, 0007, 0009, null, null, 0014 },
                { 0007, 0000, 0010, 0015, null, null },
                { 0009, 0010, 0000, 0011, null, 0002 },
                { null, 0015, 0011, 0000, 0006, null },
                { null, null, null, 0006, 0000, 0009 },
                { 0014, null, 0002, null, 0009, 0000 },
            });
            var dijkstra  = new Dijkstra(weights);
            var distances = dijkstra.GetResult(0);

            Assert.Equal(distances, new List <Dijkstra.Vertex>
            {
                new Dijkstra.Vertex(0),
                new Dijkstra.Vertex(7, 0),
                new Dijkstra.Vertex(9, 0),
                new Dijkstra.Vertex(20, 2),
                new Dijkstra.Vertex(20, 5),
                new Dijkstra.Vertex(11, 2)
            });
        }
Ejemplo n.º 10
0
    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("*************************************");
    }
Ejemplo n.º 11
0
 int v;        //从0开始~
 public adjIterator(DenseGraph graph, int v)
 {
     this.G     = graph;
     this.v     = v;
     this.index = 0;
 }