Beispiel #1
0
    private void BFS(Graph g, int v)
    {
        Queue <int> queue = new Queue <int>();

        marked[v] = true;
        queue.Enqueue(v);

        ListBag <int> adj  = g.adj[v];
        BagNode <int> node = adj.first;

        while (queue.Count != 0)
        {
            int s = queue.Dequeue();
            Debug.Log("出队:" + s);
            //遍历周围的点
            while (node.next != null)
            {
                node = node.next;
                if (!marked[node.t])
                {
                    marked[node.t] = true;
                    edgeTo[node.t] = v;
                    BFS(g, node.t);
                    //queue.Enqueue(node.t);
                }
            }
        }
    }
Beispiel #2
0
    private void Visit(EdgeWeightedGraph g, int v)
    {
        marked[v] = true;

        ListBag <Edge> list = g.Adj()[v];
        BagNode <Edge> node = list.first;

        while (node != null)
        {
            int w = node.t.Other(v);

            if (!marked[w])
            {
                //更新权值最小的边
                if (node.t.Weight() < distTo[w])
                {
                    edgeTo[w] = node.t;
                    distTo[w] = node.t.Weight();

                    //记录最近的顶点
                    if (same)
                    {
                        pq[pq.Count - 1] = w;
                    }
                    else
                    {
                        same = true;
                        pq.Add(w);
                    }
                }
            }
            node = node.next;
        }
    }
Beispiel #3
0
 public EdgeWeightedGraph(int v)
 {
     this.v = v;
     this.e = 0;
     adj    = new ListBag <Edge> [v];
     for (int i = 0; i < v; i++)
     {
         adj[i] = new ListBag <Edge>();
     }
 }
Beispiel #4
0
    public Graph(int v)
    {
        this.v = v;
        this.e = 0;

        adj = new ListBag <int> [v];
        for (int i = 0; i < v; i++)
        {
            adj[i] = new ListBag <int>();
        }
    }
Beispiel #5
0
 public async void RefreshListBag()
 {
     await Task.Run(() =>
     {
         var taskListBag = _diceDataBase.GetBagAsync();
         var listBag     = taskListBag.Result;
         ListBag.Clear();
         foreach (var item in listBag)
         {
             ListBag.Add(item);
         }
     });
 }
Beispiel #6
0
    private void DFS(Digraph g, int v)
    {
        marked[v] = true;

        ListBag <int> list = g.Adj(v);
        BagNode <int> node = list.first;

        while (node != null)
        {
            if (!marked[node.t])
            {
                DFS(g, node.t);
            }
            node = node.next;
        }
    }
Beispiel #7
0
    private void Visit(EdgeWeightedGraph g, int v)
    {
        marked[v] = true;

        ListBag <Edge> list = g.Adj()[v];
        BagNode <Edge> node = list.first;

        while (node != null)
        {
            if (!marked[node.t.Other(v)])
            {
                pq.Add(node.t);
            }
            node = node.next;
        }
    }
Beispiel #8
0
    /// <summary>
    /// 有向图取反
    /// adj可以获取每个顶点所连向的点
    /// 取反后可以获取每个顶点被谁指向
    /// </summary>
    public Digraph Reverse()
    {
        Digraph r = new Digraph(v);

        for (int i = 0; i < v; i++)
        {
            ListBag <int> list = adj[i];
            BagNode <int> node = list.first;

            while (node != null)
            {
                //把值作为key添加进新的图里
                r.AddEdge(node.t, i);
                node = node.next;
            }
        }

        return(r);
    }
Beispiel #9
0
    private void DFS(Digraph g, int v)
    {
        pre.Add(v);
        marked[v] = true;

        ListBag <int> list = g.Adj(v);
        BagNode <int> node = list.first;

        while (node != null)
        {
            if (!marked[node.t])
            {
                DFS(g, node.t);
            }
            node = node.next;
        }

        post.Add(v);
        reversePost.Push(v);
    }
Beispiel #10
0
    /// <summary>
    /// 遍历该顶点的领接表
    /// 相邻的用marked=true
    /// </summary>
    private void DFS(Graph g, int v, int w)
    {
        marked[v] = true;
        count++;

        ListBag <int> adj  = g.adj[v];
        BagNode <int> node = adj.first;

        if (node == null)
        {
            return;
        }


        while (node != null)
        {
            Debug.Log("下一个点:" + node.t);
            if (!marked[node.t])
            {
                //marked[node.t] = true;
                //count++;
                //一个点不会赋值两次
                edgeTo[node.t] = v;

                //相邻两点颜色值不同
                colors[node.t] = !colors[v];

                DFS(g, node.t, v);
            }
            else if (node.t != w)
            {
                hasCycle = true;
            }

            if (colors[node.t] == colors[v])
            {
                isTwoColor = false;
            }
            node = node.next;
        }
    }
Beispiel #11
0
    public KruskalMST(EdgeWeightedGraph g)
    {
        mst = new List <Edge>();
        pq  = new List <Edge>();

        ListBag <Edge>[] list = g.Adj();

        for (int i = 0; i < list.Length; i++)
        {
            ListBag <Edge> e    = list[i];
            BagNode <Edge> node = e.first;

            while (node != null)
            {
                //Debug.Log(node.t.Weight());
                pq.Add(node.t);
                node = node.next;
            }
        }

        //每次添加一条权值最小的边
        //多花一个数组空间检测是否形成环
        while (pq.Count != 0 && mst.Count < g.V() - 1)
        {
            int min = GetMinWeight();

            Edge e = pq[min];
            pq.RemoveAt(min);

            if (!Connected(e))
            {
                int v = e.Either();
                int w = e.Other(v);

                //Debug.Log(v + "," + w);

                mst.Add(e);
            }
        }
    }
Beispiel #12
0
    private void DFS(Graph g, int v)
    {
        marked[v] = true;
        id[v]     = count;

        ListBag <int> adj  = g.adj[v];
        BagNode <int> node = adj.first;

        if (node == null)
        {
            return;
        }

        while (node.next != null)
        {
            node = node.next;
            if (!marked[node.t])
            {
                DFS(g, node.t);
            }
        }
    }
Beispiel #13
0
    private void DFS(Digraph g, int v)
    {
        onStack[v] = true;
        marked[v]  = true;

        ListBag <int> list = g.Adj(v);
        BagNode <int> node = list.first;

        //遍历v的有向路径
        while (node != null)
        {
            if (HasCycle())
            {
                return;
            }
            else if (!marked[node.t])
            {
                edgeTo[node.t] = v;
                DFS(g, node.t);
            }
            else if (onStack[node.t])
            {
                //onStack记录了当前这条路径中已经遍历过的顶点
                //所以重复的顶点肯定形成了有向环
                cycle = new List <int>();
                for (int i = v; i != node.t; i = edgeTo[i])
                {
                    cycle.Add(i);
                }

                cycle.Add(node.t);
                cycle.Add(v);
            }
        }
        onStack[v] = false;
    }
Beispiel #14
0
 public DirectedDFS(Digraph g, ListBag <int> sources)
 {
     marked = new bool[g.V()];
 }