391 lines
12 KiB
C#
391 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Encompass
|
|
{
|
|
internal class DirectedGraph<TNode> where TNode : IEquatable<TNode>
|
|
{
|
|
protected HashSet<TNode> nodes = new HashSet<TNode>();
|
|
protected Dictionary<TNode, HashSet<TNode>> neighbors = new Dictionary<TNode, HashSet<TNode>>();
|
|
|
|
protected HashSet<(TNode, TNode)> edges = new HashSet<(TNode, TNode)>();
|
|
|
|
public IEnumerable<TNode> Nodes => nodes;
|
|
public IEnumerable<(TNode, TNode)> Edges => edges;
|
|
|
|
public void AddNode(TNode node)
|
|
{
|
|
if (!Exists(node))
|
|
{
|
|
nodes.Add(node);
|
|
neighbors.Add(node, new HashSet<TNode>());
|
|
}
|
|
}
|
|
|
|
public void AddNodes(params TNode[] nodes)
|
|
{
|
|
foreach (var node in nodes)
|
|
{
|
|
AddNode(node);
|
|
}
|
|
}
|
|
|
|
public void RemoveNode(TNode node)
|
|
{
|
|
CheckNodes(node);
|
|
|
|
var edgesToRemove = new List<(TNode, TNode)>();
|
|
|
|
foreach (var entry in neighbors)
|
|
{
|
|
if (entry.Value.Contains(node))
|
|
{
|
|
edgesToRemove.Add((entry.Key, node));
|
|
}
|
|
}
|
|
|
|
foreach (var edge in edgesToRemove)
|
|
{
|
|
RemoveEdge(edge.Item1, edge.Item2);
|
|
}
|
|
|
|
nodes.Remove(node);
|
|
neighbors.Remove(node);
|
|
}
|
|
|
|
public void RemoveEdge(TNode v, TNode u)
|
|
{
|
|
CheckEdge(v, u);
|
|
neighbors[v].Remove(u);
|
|
edges.Remove((v, u));
|
|
}
|
|
|
|
public void AddEdge(TNode v, TNode u)
|
|
{
|
|
CheckNodes(v, u);
|
|
if (Exists(v, u)) { throw new ArgumentException($"Edge between {v} and {u} already exists in the graph"); }
|
|
|
|
if (v.Equals(u)) { throw new ArgumentException("Self-edges are not allowed in a simple graph. Use a multigraph instead"); }
|
|
|
|
neighbors[v].Add(u);
|
|
edges.Add((v, u));
|
|
}
|
|
|
|
public bool Exists(TNode node)
|
|
{
|
|
return nodes.Contains(node);
|
|
}
|
|
|
|
public bool Exists(TNode v, TNode u)
|
|
{
|
|
CheckNodes(v, u);
|
|
return edges.Contains((v, u));
|
|
}
|
|
|
|
protected void CheckNodes(params TNode[] givenNodes)
|
|
{
|
|
foreach (var node in givenNodes)
|
|
{
|
|
if (!Exists(node))
|
|
{
|
|
throw new System.ArgumentException($"Vertex {node} does not exist in the graph");
|
|
}
|
|
}
|
|
}
|
|
|
|
protected void CheckEdge(TNode v, TNode u)
|
|
{
|
|
CheckNodes(v, u);
|
|
if (!Exists(v, u)) { throw new ArgumentException($"Edge between vertex {v} and vertex {u} does not exist in the graph"); }
|
|
}
|
|
|
|
public IEnumerable<TNode> Neighbors(TNode node)
|
|
{
|
|
CheckNodes(node);
|
|
return neighbors[node];
|
|
}
|
|
|
|
public DirectedGraph<TNode> Clone()
|
|
{
|
|
var clone = new DirectedGraph<TNode>();
|
|
clone.AddNodes(Nodes.ToArray());
|
|
|
|
foreach (var v in Nodes)
|
|
{
|
|
foreach (var n in Neighbors(v))
|
|
{
|
|
clone.AddEdge(v, n);
|
|
}
|
|
}
|
|
|
|
return clone;
|
|
}
|
|
|
|
public DirectedGraph<TNode> SubGraph(params TNode[] subVertices)
|
|
{
|
|
var subGraph = new DirectedGraph<TNode>();
|
|
subGraph.AddNodes(subVertices.ToArray());
|
|
|
|
foreach (var n in Nodes)
|
|
{
|
|
if (Nodes.Contains(n))
|
|
{
|
|
var neighbors = Neighbors(n);
|
|
foreach (var u in neighbors)
|
|
{
|
|
if (subVertices.Contains(u))
|
|
{
|
|
subGraph.AddEdge(n, u);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return subGraph;
|
|
}
|
|
|
|
private IEnumerable<TNode> PostorderNodeDFSHelper(HashSet<TNode> discovered, TNode v)
|
|
{
|
|
discovered.Add(v);
|
|
|
|
foreach (var neighbor in Neighbors(v))
|
|
{
|
|
if (!discovered.Contains(neighbor))
|
|
{
|
|
foreach (var node in PostorderNodeDFSHelper(discovered, neighbor))
|
|
{
|
|
yield return node;
|
|
}
|
|
}
|
|
}
|
|
|
|
yield return v;
|
|
}
|
|
|
|
protected IEnumerable<TNode> PostorderNodeDFS()
|
|
{
|
|
var dfsDiscovered = new HashSet<TNode>();
|
|
|
|
foreach (var node in Nodes)
|
|
{
|
|
if (!dfsDiscovered.Contains(node))
|
|
{
|
|
foreach (var thing in PostorderNodeDFSHelper(dfsDiscovered, node))
|
|
{
|
|
yield return thing;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public IEnumerable<TNode> TopologicalSort()
|
|
{
|
|
return PostorderNodeDFS().Reverse();
|
|
}
|
|
|
|
public bool Cyclic()
|
|
{
|
|
return StronglyConnectedComponents().Any((scc) => scc.Count() > 1);
|
|
}
|
|
|
|
public IEnumerable<IEnumerable<TNode>> SimpleCycles()
|
|
{
|
|
void unblock(TNode thisnode, HashSet<TNode> blocked, Dictionary<TNode, HashSet<TNode>> B) //refactor to remove closure
|
|
{
|
|
var stack = new Stack<TNode>();
|
|
stack.Push(thisnode);
|
|
|
|
while (stack.Count > 0)
|
|
{
|
|
var node = stack.Pop();
|
|
if (blocked.Contains(thisnode))
|
|
{
|
|
blocked.Remove(thisnode);
|
|
if (B.ContainsKey(node))
|
|
{
|
|
foreach (var n in B[node])
|
|
{
|
|
if (!stack.Contains(n))
|
|
{
|
|
stack.Push(n);
|
|
}
|
|
}
|
|
B[node].Clear();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
List<List<TNode>> result = new List<List<TNode>>();
|
|
var subGraph = Clone();
|
|
|
|
var sccs = new Stack<IEnumerable<TNode>>();
|
|
foreach (var scc in StronglyConnectedComponents())
|
|
{
|
|
sccs.Push(scc);
|
|
}
|
|
|
|
while (sccs.Count > 0)
|
|
{
|
|
var scc = new Stack<TNode>(sccs.Pop());
|
|
var startNode = scc.Pop();
|
|
var path = new Stack<TNode>();
|
|
path.Push(startNode);
|
|
var blocked = new HashSet<TNode>
|
|
{
|
|
startNode
|
|
};
|
|
var closed = new HashSet<TNode>();
|
|
var B = new Dictionary<TNode, HashSet<TNode>>();
|
|
var stack = new Stack<(TNode, Stack<TNode>)>();
|
|
stack.Push((startNode, new Stack<TNode>(subGraph.Neighbors(startNode))));
|
|
|
|
while (stack.Count > 0)
|
|
{
|
|
var entry = stack.Peek();
|
|
var thisnode = entry.Item1;
|
|
var neighbors = entry.Item2;
|
|
|
|
if (neighbors.Count > 0)
|
|
{
|
|
var nextNode = neighbors.Pop();
|
|
|
|
if (nextNode.Equals(startNode))
|
|
{
|
|
var resultPath = new List<TNode>();
|
|
foreach (var v in path)
|
|
{
|
|
resultPath.Add(v);
|
|
}
|
|
result.Add(resultPath);
|
|
foreach (var v in path)
|
|
{
|
|
closed.Add(v);
|
|
}
|
|
}
|
|
else if (!blocked.Contains(nextNode))
|
|
{
|
|
path.Push(nextNode);
|
|
stack.Push((nextNode, new Stack<TNode>(subGraph.Neighbors(nextNode))));
|
|
closed.Remove(nextNode);
|
|
blocked.Add(nextNode);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
if (neighbors.Count == 0)
|
|
{
|
|
if (closed.Contains(thisnode))
|
|
{
|
|
unblock(thisnode, blocked, B);
|
|
}
|
|
else
|
|
{
|
|
foreach (var neighbor in subGraph.Neighbors(thisnode))
|
|
{
|
|
if (!B.ContainsKey(neighbor))
|
|
{
|
|
B[neighbor] = new HashSet<TNode>();
|
|
}
|
|
B[neighbor].Add(thisnode);
|
|
}
|
|
}
|
|
|
|
stack.Pop();
|
|
path.Pop();
|
|
}
|
|
}
|
|
|
|
subGraph.RemoveNode(startNode);
|
|
var H = subGraph.SubGraph(scc.ToArray());
|
|
var HSccs = H.StronglyConnectedComponents();
|
|
foreach (var HScc in HSccs)
|
|
{
|
|
sccs.Push(HScc);
|
|
}
|
|
}
|
|
|
|
return result.Distinct(new SimpleCycleComparer<TNode>());
|
|
}
|
|
|
|
protected IEnumerable<IEnumerable<TNode>> StronglyConnectedComponents()
|
|
{
|
|
var preorder = new Dictionary<TNode, uint>();
|
|
var lowlink = new Dictionary<TNode, uint>();
|
|
var sccFound = new Dictionary<TNode, bool>();
|
|
var sccQueue = new Stack<TNode>();
|
|
|
|
uint preorderCounter = 0;
|
|
|
|
foreach (var source in Nodes)
|
|
{
|
|
if (!sccFound.ContainsKey(source))
|
|
{
|
|
var queue = new Stack<TNode>();
|
|
queue.Push(source);
|
|
|
|
while (queue.Count > 0)
|
|
{
|
|
var v = queue.Peek();
|
|
if (!preorder.ContainsKey(v))
|
|
{
|
|
preorderCounter++;
|
|
preorder[v] = preorderCounter;
|
|
}
|
|
|
|
var done = true;
|
|
var vNeighbors = Neighbors(v);
|
|
foreach (var w in vNeighbors)
|
|
{
|
|
if (!preorder.ContainsKey(w))
|
|
{
|
|
queue.Push(w);
|
|
done = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (done)
|
|
{
|
|
lowlink[v] = preorder[v];
|
|
foreach (var w in vNeighbors)
|
|
{
|
|
if (!sccFound.ContainsKey(w))
|
|
{
|
|
if (preorder[w] > preorder[v])
|
|
{
|
|
lowlink[v] = Math.Min(lowlink[v], lowlink[w]);
|
|
}
|
|
else
|
|
{
|
|
lowlink[v] = Math.Min(lowlink[v], preorder[w]);
|
|
}
|
|
}
|
|
}
|
|
queue.Pop();
|
|
if (lowlink[v] == preorder[v])
|
|
{
|
|
sccFound[v] = true;
|
|
var scc = new List<TNode>() { v };
|
|
while (sccQueue.Count > 0 && preorder[sccQueue.Peek()] > preorder[v])
|
|
{
|
|
var k = sccQueue.Pop();
|
|
sccFound[k] = true;
|
|
scc.Add(k);
|
|
}
|
|
|
|
yield return scc;
|
|
}
|
|
else
|
|
{
|
|
sccQueue.Push(v);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|