private void DfsVisit(GraphBase g, int u, int p) { _marked[u] = true; foreach (var v in g.Adj(u)) { if (_hasCycle) { break; } if (!_marked[v]) { _parents[v] = u; DfsVisit(g, v, u); } else if (v != p) { _cycle = new Stack <int>(); _cycle.Push(v); while (u != v) { _cycle.Push(u); u = _parents[u]; } _cycle.Push(v); _hasCycle = true; break; } } }
private void DfsVisit(GraphBase g, int u, int p) { _vertices[u].Color = Color.Gray; foreach (var v in g.Adj(u)) { if (_hasCycle) { break; } if (_vertices[v].Color != Color.Gray) { _vertices[v].Parent = _vertices[u]; DfsVisit(g, v, u); } else if (v != p) { _cycle = new Stack <int>(); _cycle.Push(v); while (u != v) { _cycle.Push(u); u = _vertices[u].Parent.Val; } _cycle.Push(v); _hasCycle = true; break; } } }
//------converters public void Convert2anothertype() { GraphBase <VertexType, EdgeType> ng;// will conatain new graph referance; if (g is MatrixBasedGraph <VertexType, EdgeType> ) { ng = new ListBasedGraphSE <VertexType, EdgeType>(VertexCount, g.IsDirected); } else { ng = new MatrixBasedGraph <VertexType, EdgeType>(VertexCount, g.IsDirected); } //objectless functional calls are routed to g object for (int i = 0; i < VertexCount; i++) { for (int j = 0; j < VertexCount; j++) { if (EdgeExists(i, j)) { ng.AddEdge(i, j, GetEdgeWeight(i, j)); } } } g = ng; PrepareIterator(0); }
public GraphController(int VertexCount, bool listbased, bool directed) { if (listbased) { g = new ListBasedGraphSE <VertexType, EdgeType>(VertexCount, directed); } else { g = new MatrixBasedGraph <VertexType, EdgeType>(VertexCount, directed); } PrepareIterator(0); }
public Cycle1(GraphBase g) { _marked = new bool[g.V]; _parents = new int[g.V]; for (int i = 0; i < g.V; i++) { if (_hasCycle) { break; } if (!_marked[i]) { DfsVisit(g, i, i); } } }
public Cycle(GraphBase g) { _vertices = new Vertex[g.V]; for (int i = 0; i < g.V; i++) { _vertices[i] = new Vertex { Color = Color.White, Val = i }; } for (int i = 0; i < g.V; i++) { if (_hasCycle) { break; } if (_vertices[i].Color == Color.White) { DfsVisit(g, i, i); } } }