コード例 #1
0
    private void DenseGraphBSF(int i)
    {
        int   beginU = i;
        Queue q      = new Queue();

        q.Enqueue(beginU);

        while (q.Count != 0)
        {
            int u = (int)q.Dequeue();
            Debug.Log("正在访问的节点 :" + u);

            DenseGraph.adjIterator iter = new DenseGraph.adjIterator(dgraph, u);
            int j = 0;
            for (int v = iter.begin(); !iter.IsEnd(); v = iter.next(), j++)
            {
                if (v == 1 && !book [j])
                {
                    book [j] = true;
                    from [j] = u;
                    q.Enqueue(j);
                }
            }
        }
    }
コード例 #2
0
    private void DenseGraphDFS(int u)
    {
        id [u] = count;
        //标记
        Debug.Log("正在访问的节点 :" + u);
        //
        DenseGraph.adjIterator iter = new DenseGraph.adjIterator(dgraph, u);
        int j = 0;

        for (int v = iter.begin(); !iter.IsEnd(); v = iter.next(), j++)
        {
            if (v == 1 && !book[j])
            {
                from [j] = u;
                book[j]  = true;
                DenseGraphDFS(j);
            }
        }
    }
コード例 #3
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("*************************************");
    }