private void Dfs(IGraph g, int v) { _marked[v] = true; _count++; foreach (int i in g.Adj(v)) { if (!_marked[i]) { Dfs(g, i); } } }
private void dfs(int v) { visited[v] = true; foreach (int i in G.Adj(v)) { if (!visited[i]) { from[i] = v; // 表示i点从v点来 dfs(i); } } }
private void Dfs(IGraph graph, int v, Action <int> action) { action(v); visited[v] = true; foreach (int w in graph.Adj(v)) { if (!visited[w]) { Dfs(graph, w, action); } } }
private void Dfs(IGraph graph, int v) { marked[v] = true; CountNum++; foreach (int w in graph.Adj(v)) { if (!marked[w]) //如果没有走过,则继续深度遍历 { edgeTo[w] = v; Dfs(graph, w); } } }
private void dfs(IGraph graph, int v) { _marked[v] = true; foreach (int w in graph.Adj(v)) { if (_marked[w]) { continue; } _edgeTo[w] = v; dfs(graph, w); } }
private void dfs(IGraph graph, int v) { _marked[v] = true; // отмечаем вершину _id[v] = _count; // поставляем номер компонента foreach (int i in graph.Adj(v)) { if (_marked[i]) { continue; } dfs(graph, i); } }
private void Dfs(IGraph g, int v) { this.marked[v] = true; IEnumerable <int> adjacent = g.Adj(v); foreach (int w in adjacent) { if (!marked[w]) { Dfs(g, w); edgeTo[w] = v; } } }
// 深度遍历v的所有邻边 private void dfs(int v) { visted[v] = true; id[v] = count; IEnumerable <int> iterator = G.Adj(v); foreach (int i in iterator) { if (!visted[i]) { dfs(i); } } }
private void dfs(IGraph graph, int v, int u) { _marked[v] = true; foreach (int w in graph.Adj(v)) { if (!_marked[w]) { dfs(graph, w, v); } else if (w != u) { _hasCycle = true; } } }
private void dfs(IGraph g, int v) { //前向排序是顶点的顺序遍历之前的顺序 pre.Enqueue(v); marked[v] = true; foreach (int w in g.Adj(v)) { if (!marked[w]) { dfs(g, w); } } post.Enqueue(v); //这个是逆序的 reversePost.Push(v); //递归后将顶点压栈 }
//计算自环的个数 public static int NumberOfSelfLoops(IGraph g) { int count = 0; for (int v = 0; v < g.V; v++) { foreach (int w in g.Adj(v)) { if (v == w) { count++; } } } return(count / 2); //每条边都被记过两次 }
private void Dfs(IGraph g, int v, int u) { marked[v] = true; id[v] = count; foreach (int w in g.Adj(v)) { if (!marked[w]) { Dfs(g, w, v); } else if (w != u) { HasCycle = true; } } }
private void dfs(IGraph graph, int v) { _marked[v] = true; foreach (int w in graph.Adj(v)) { if (!_marked[w]) { _color[w] = !_color[v]; dfs(graph, w); } else if (_color[w] == _color[v]) { _isTwoColorable = false; } } }
public static int NumberOfSelfLoops(this IGraph graph) { int count = 0; for (int i = 0; i < graph.V; i++) { foreach (int j in graph.Adj(i)) { if (j == i) { count++; } } } //In graph theory, the degree (or valency) of a vertex of a graph // is the number of edges incident to the vertex, // WITH LOOPS COUNTED TWICE. return(count / 2); }
int[] ord; // 记录路径节点的次序 public ShortestPath(IGraph g, int s) { this.g = g; this.s = s; if (s < 0 && s >= g.V()) { throw new Exception("s is illegal"); } this.visted = new bool[g.V()]; this.from = new int[g.V()]; this.ord = new int[g.V()]; for (int i = 0; i < g.V(); i++) { from[i] = -1; ord[i] = -1; } // 无向图的最短路径算法 Queue <int> queue = new Queue <int>(); queue.Enqueue(s); visted[s] = true; ord[s] = 0; while (queue.Count > 0) { int i = queue.Dequeue(); foreach (var j in g.Adj(i)) { if (!visted[j]) { queue.Enqueue(j); visted[j] = true; from[j] = i; ord[j] = ord[i] + 1; } } } }
private bool Dfs(IGraph graph, int v, int destination, bool[] visited) { if (v == destination) { return(true); } visited[v] = true; foreach (int w in graph.Adj(v)) { if (!visited[w]) { bool pathExists = Dfs(graph, w, destination, visited); if (pathExists) { return(true); } } } return(false); }
private void Bfs(IGraph g, int s) { var q = new Queue <int>(); _marked[s] = true; q.Enqueue(s); while (q.Count > 0) { int v = q.Dequeue(); foreach (int w in g.Adj(v)) { if (!_marked[w]) { _edgeTo[w] = v; _marked[w] = true; q.Enqueue(w); } } } }
private void Bfs(IGraph g, int s) { var queue = new Chapter1.Queue <int>(); marked[s] = true; //起点被标记 queue.Enqueue(s); while (!queue.IsEmpty) { int v = queue.Dequeue(); //从队列中删除一个顶点 foreach (int w in g.Adj(v)) { if (!marked[w]) { edgeTo[w] = v; marked[w] = true; queue.Enqueue(w); } } } }
void BFS(IGraph g, int s) { var queue = new Queue <int>(); queue.Enqueue(s); while (queue.Count != 0) { var count = queue.Count; while (count != 0) { var v = queue.Dequeue(); foreach (var w in g.Adj(v).Where(w => !marked[w])) { marked[w] = true; edgeTo[w] = v; queue.Enqueue(w); } count -= 1; } } }
/// <summary> /// Performs action <paramref name="action"/> on each vertex of graph <paramref name="graph"/>, starting from vertex <paramref name="start"/>. /// </summary> /// <param name="graph">A graph that will be traversed.</param> /// <param name="start">A vertex from which the traversal will start.</param> /// <param name="action">Action that will be performed on each visited vertice.</param> public static void Traverse(this IGraph graph, int start, Action <int> action) { bool[] visited = new bool[graph.V]; Queue <int> toVisit = new Queue <int>(); toVisit.Enqueue(start); visited[start] = true; while (toVisit.Count > 0) { int current = toVisit.Dequeue(); IEnumerable <int> children = graph.Adj(current); foreach (int child in children) { if (!visited[child]) { toVisit.Enqueue(child); visited[child] = true; } } action(current); } }
private static void Graph() { Graph graph = new Graph(new Scanner(new StreamReader(File.OpenRead("tinyG.txt")))); Graph graph2 = new Graph(new Scanner(new StreamReader(File.OpenRead("tinyCG.txt")))); StdOut.Println("输入一个数字: "); int s = StdIn.ReadInt(); ISearch search = new DepthFirstSearch(graph, s); for (int v = 0; v < graph.V; v++) { if (search.Marked(v)) { StdOut.Print(v + " "); } } StdOut.Println(); StdOut.Println(); StdOut.Println(); ICC cc = new DepthFirstSearch(graph, s).InitCC(); int M = cc.CountCC(); StdOut.Println(M + " components"); Bag <int>[] components = new Bag <int> [M]; for (int i = 0; i < M; i++) { components[i] = new Bag <int>(); } for (int v = 0; v < graph.V; v++) { components[cc.Id(v)].Add(v); } for (int i = 0; i < M; i++) { foreach (int v in components[i]) { StdOut.Print(v + " "); } StdOut.Println(); } StdOut.Println(cc.HasCycle); if (search.Count() != graph.V) { StdOut.Print("Not "); } StdOut.Println("connected"); IPath path = new DepthFirstSearch(graph2, s); for (int v = 0; v < graph2.V; v++) { StdOut.Print(s + " to " + v + ": "); if (path.HasPathTo(v)) { foreach (int x in path.PathTo(v)) { if (x == s) { StdOut.Print(x); } else { StdOut.Print("-" + x); } } } StdOut.Println(); } //符号图 string filename = "routes.txt"; string filename2 = "movies.txt"; string delim = "/"; ISymbolGraph sg = new SymbolGraph(filename2, delim); IGraph g = sg.G; while (StdIn.HasNextLine()) { StdOut.Println("输入查找的字符串"); string source = StdIn.ReadLine(); foreach (int w in g.Adj(sg.Index(source))) { StdOut.Println(" " + sg.Name(w)); } } }