private void Dfs(int v, int parent) { _visited[v] = true; _order[v] = _cnt; _low[v] = _order[v]; _cnt++; int child = 0; foreach (var w in _iAdjacency.GetAllContiguousEdge(v)) { if (!_visited[w]) { Dfs(w, v); _low[v] = Math.Min(_low[v], _low[w]); if (v != parent && _low[w] >= _order[v]) { CutPoints.Add(v); } child++; if (v == parent && child > 1) { CutPoints.Add(v); } } else if (w != parent) { _low[v] = Math.Min(_low[v], _low[w]); } } }
/// <summary> /// 欧拉路径(Hierholzer算法) /// </summary> /// <returns></returns> public List <int> EulerLoopPath() { List <int> path = new List <int>(); if (!IsExistsEulerLoop()) { return(path); } Stack <int> stack = new Stack <int>(); IAdjacency adjacency = _iAdjacency.Clone(); int cur = 0; stack.Push(cur); while (stack.Count > 0) { if (adjacency.Dgree(cur) != 0) { stack.Push(cur); int w = adjacency.GetAllContiguousEdge(cur).First(); adjacency.RemoveEdge(cur, w); cur = w; } else { path.Add(cur); cur = stack.Pop(); } } return(path); }
private bool Dfs(int v, int parent, int left) { _visited[v] = true; _pre[v] = parent; left--; foreach (var w in _iAdjacency.GetAllContiguousEdge(v)) { if (!_visited[w]) { if (Dfs(w, v, left)) { return(true); } } else if (left == 0) { _end = v; return(true); } } _visited[v] = false; left++; return(false); }
private bool Dfs(int v) { _visited[v] = true; foreach (var w in _iAdjacency.GetAllContiguousEdge(v)) { if (!_visited[w]) { _visited[w] = true; if (_match[w] == -1 || Dfs(w)) { _match[v] = w; _match[w] = v; return(true); } } } return(false); }
public void Dfs(int v, int count) { _visitedForConnectedWeight[v] = count; foreach (var w in _origion.GetAllContiguousEdge(v)) { if (_visitedForConnectedWeight[w] == -1) { Dfs(w, count); } } }
public void Dfs(int v) { _visited[v] = true; foreach (var w in _iAdjacency.GetAllContiguousEdge(v)) { if (!_visited[w]) { Dfs(w); } } _post.Add(v); }
/// <summary> /// 深度优先遍历 /// </summary> /// <param name="v"></param> /// <param name="connectedComponentIndex">第几个联通分量的索引</param> private void Dfs(int v, int connectedComponentIndex) { _visited[v] = connectedComponentIndex; PreOrder.Add(v); foreach (var w in _adjacency.GetAllContiguousEdge(v)) { if (_visited[w] == -1) { Dfs(w, connectedComponentIndex); } } PostOrder.Add(v); }
private void Bfs(int v) { _visited[v] = true; Queue <int> queue = new Queue <int>(); queue.Enqueue(v); while (queue.Count > 0) { int s = queue.Dequeue(); Order.Add(s); foreach (var w in _adjacency.GetAllContiguousEdge(s)) { if (!_visited[w]) { queue.Enqueue(w); _visited[w] = true; } } } }
/// <summary> /// 深度优先遍历并且记录是否有环 /// </summary> /// <param name="v"></param> /// <param name="parent"></param> /// <returns></returns> private bool Dfs(int v, int parent) { _visited[v] = true; foreach (var w in _adjacency.GetAllContiguousEdge(v)) { if (!_visited[w]) { if (Dfs(w, v)) { return(true); } else if (w != parent) { return(true); } } } return(false); }
/// <summary> /// 深度优先遍历并且检测二分图冲突 /// </summary> /// <param name="v"></param> /// <param name="color"></param> /// <returns></returns> private bool Dfs(int v, int color) { _visited[v] = true; _colors[v] = color; foreach (var w in _adjacency.GetAllContiguousEdge(v)) { if (!_visited[w]) { if (!Dfs(w, 1 - color)) { return(false); } } else if (_colors[v] == _colors[w]) { return(false); } } return(true); }
/// <summary> /// 从s开始遍历记录父子关系到t为止 /// </summary> /// <param name="s"></param> /// <param name="parent"></param> /// <returns></returns> private bool Dfs(int s, int parent) { _visited[s] = true; PreNodes[s] = parent; if (s == T) { return(true); } foreach (var v in _adjacency.GetAllContiguousEdge(s)) { if (!_visited[v]) { if (Dfs(v, s)) { return(true); } } } return(false); }
public ToopSort(IAdjacency iAdjacency) { _iAdjacency = iAdjacency; if (!iAdjacency.Directed) { throw new Exception("directed graph can be support"); } Result = new List <int>(); Queue <int> queue = new Queue <int>(); int[] indgree = new int[iAdjacency.V]; for (int i = 0; i < iAdjacency.V; i++) { indgree[i] = iAdjacency.InDegree(i); if (iAdjacency.InDegree(i) == 0) { queue.Enqueue(i); } } while (queue.Count > 0) { int v = queue.Dequeue(); Result.Add(v); foreach (var w in _iAdjacency.GetAllContiguousEdge(v)) { indgree[w]--; if (indgree[w] == 0) { queue.Enqueue(w); } } } if (Result.Count != iAdjacency.V) { IsHaveCircle = true; Result.Clear(); } }
public BinaryPartGraphMatch(IAdjacency iAdjacency) { _iAdjacency = iAdjacency; BinaryPartitionDetection binaryPartitionDetection = new BinaryPartitionDetection(iAdjacency); if (!binaryPartitionDetection.IsBinaryPartition) { throw new Exception("you must use two partite graph"); } int[] colors = binaryPartitionDetection.Colors; WeightGraph weightGraph = new WeightGraph(iAdjacency.V + 2, true); for (int v = 0; v < iAdjacency.V; v++) { if (colors[v] == 0) { weightGraph.AddEdge(iAdjacency.V, v, 1); } if (colors[v] == 1) { weightGraph.AddEdge(v, iAdjacency.V + 1, 1); } foreach (var w in iAdjacency.GetAllContiguousEdge(v)) { if (colors[v] == 0) { weightGraph.AddEdge(v, w, 1); } else if (colors[v] == 1) { weightGraph.AddEdge(w, v, 1); } } } Edmonds_Karp edmondsKarp = new Edmonds_Karp(weightGraph, iAdjacency.V, iAdjacency.V + 1); MaxMatch = edmondsKarp.MaxFlow; IsPerfectMatch = MaxMatch * 2 == iAdjacency.V; }
private bool Dfs(int v) { _visited[v] = true; _onPath[v] = true; foreach (var w in _iAdjacency.GetAllContiguousEdge(v)) { if (!_visited[w]) { if (Dfs(w)) { return(true); } else if (_onPath[w]) { return(true); } } } _onPath[v] = false; return(false); }
private void Dfs(int v, int parent) { _visited[v] = true; _order[v] = _cnt; _low[v] = _order[v]; _cnt++; foreach (var w in _iaAdjacency.GetAllContiguousEdge(v)) { if (!_visited[w]) { Dfs(w, v); _low[v] = Math.Min(_low[v], _low[w]); if (_low[w] > _order[v]) { BridgeEdge.Add(new Edge(w, v)); } } else if (w != parent) { _low[v] = Math.Min(_low[v], _low[w]); } } }
/// <summary> /// 广度优先遍历求路径与距离 /// </summary> /// <param name="v"></param> private void Bfs(int v) { Queue <int> queue = new Queue <int>(); _visited[v] = true; queue.Enqueue(v); _pre[v] = v; _distance[v] = 0; while (queue.Count > 0) { int s = queue.Dequeue(); foreach (var w in _iaAdjacency.GetAllContiguousEdge(s)) { if (!_visited[w]) { _visited[w] = true; queue.Enqueue(w); _pre[w] = s; _distance[w] = _distance[s] + 1; } } } }
public bool Dfs(int v, int parent) { _visited[v] = true; _pre[v] = parent; foreach (var w in _iAdjacency.GetAllContiguousEdge(v)) { if (!_visited[w]) { if (Dfs(w, v)) { return(true); } } else if (w == 0 && AllVisited()) { _end = v; return(true); } } _visited[v] = false; return(false); }