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); } } } }
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; } }
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>(); } }
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>(); } }
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); } }); }
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; } }
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; } }
/// <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); }
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); }
/// <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; } }
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); } } }
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); } } }
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; }
public DirectedDFS(Digraph g, ListBag <int> sources) { marked = new bool[g.V()]; }