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; }
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); } } } }
public static int Degree(Graph g, int v) { return g.Adjacent(v).Count(); }