public BFSPaths(Graph g, int s) { _marked = new bool[g.V]; _edgeTo = new int[g.V]; _s = s; bfs(g, s); }
public static int NumberOfSelfLoops(Graph g) { int count = 0; g.V.Times(v => { foreach (int w in g.Adjacent(v)) { if (v == w) count++; } }); return count / 2; }
public static int MaxDegree(Graph g) { int max = 0; g.V.Times(v => { if (Degree(g, v) > max) { max = Degree(g, v); } }); return max; }
private void dfs(Graph g, int v) { _marked[v] = true; foreach (var adj in g.Adjacent(v)) { if (!_marked[adj]) { _edgeTo[adj] = v; dfs(g, adj); } } }
private void bfs(Graph g, int s) { var queue = new Queue<int>(); _marked[s] = true; queue.Enqueue(s); while(queue.Count>0) { int v = queue.Dequeue(); foreach(int w in g.Adjacent(v)) { if(!_marked[w]) { _edgeTo[w] = v; _marked[w] = true; queue.Enqueue(w); } } } }
static void Main(string[] args) { Graph g = new Graph(9); g.AddEdge(0, 1); g.AddEdge(0, 3); g.AddEdge(1, 2); g.AddEdge(1, 4); g.AddEdge(2, 5); g.AddEdge(3, 4); g.AddEdge(3, 6); g.AddEdge(4, 5); g.AddEdge(4, 7); g.AddEdge(5, 8); g.AddEdge(6, 7); g.AddEdge(7, 8); var p = new BFSPaths(g, 0); p.PathTo(8); }
public static int Degree(Graph g, int v) { return g.Adjacent(v).Count(); }
public static int AverageDegree(Graph g) { return 2 * g.E / g.V; }