コード例 #1
0
    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);
        }
    }
コード例 #2
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("*************************************");
    }