public static void main() { DirectedGraph g = new DirectedGraph(13); g.AddEdge(4, 2); g.AddEdge(2, 3); g.AddEdge(3, 2); g.AddEdge(6, 0); g.AddEdge(0, 1); g.AddEdge(2, 0); g.AddEdge(11, 12); g.AddEdge(12, 9); g.AddEdge(9, 10); g.AddEdge(9, 11); g.AddEdge(7, 9); g.AddEdge(10, 12); g.AddEdge(11, 4); g.AddEdge(4, 3); g.AddEdge(3, 5); g.AddEdge(6, 8); g.AddEdge(8, 6); g.AddEdge(5, 4); g.AddEdge(0, 5); g.AddEdge(6, 4); g.AddEdge(6, 9); g.AddEdge(7, 6); DirectedGraphDepthFirst dp = new DirectedGraphDepthFirst(g, 2); }
public static void main() { DirectedGraph g = new DirectedGraph(13); g.AddEdge(4, 2); g.AddEdge(2, 3); g.AddEdge(3, 2); g.AddEdge(6, 0); g.AddEdge(0, 1); g.AddEdge(2, 0); g.AddEdge(11, 12); g.AddEdge(12, 9); g.AddEdge(9, 10); g.AddEdge(9, 11); g.AddEdge(7, 9); g.AddEdge(10, 12); g.AddEdge(11, 4); g.AddEdge(4, 3); g.AddEdge(3, 5); g.AddEdge(6, 8); g.AddEdge(8, 6); g.AddEdge(5, 4); g.AddEdge(0, 5); g.AddEdge(6, 4); g.AddEdge(6, 9); g.AddEdge(7, 6); DirectedGraphBreadthFirst dp = new DirectedGraphBreadthFirst(g); bool flag = dp.hasPath(g, 0, 6); Console.WriteLine(flag); }
public int[] getPath(DirectedGraph g, int vertex) { Queue<int> q = new Queue<int>(); q.Enqueue(vertex); //set up the entire distance matrix with -1 except for the vertex node for (int i = 0; i < g.getTotalVertices(); i++) { distanceMatrix[i] = -1; } //set up current vertex with 0 because there will be no distance distanceMatrix[vertex] = 0; while (q.Count > 0) { int v = q.Dequeue(); Console.Write(v); //this is the breadth first traversal ArrayList adjacent = g.Adjacent(v); //loop through all the adjacent nodes, if the currentVertex is still -1, then go in and do process for (int i = 0; i < adjacent.Count; i++) { int currentVertex = (int)adjacent[i]; if (distanceMatrix[currentVertex] == -1) { //add one to the distanceMatrix here because it's going to be from v to the current path distanceMatrix[currentVertex] = distanceMatrix[v] + 1; pathMatrix[currentVertex] = v; q.Enqueue(currentVertex); } } } return distanceMatrix; }
public bool hasPath(DirectedGraph g, int vertex1, int vertex2) { int[] vertices = getPath(g, vertex1); if (vertices[vertex2] > 0) { return true; } return false; }
public void dfs(DirectedGraph g, int vertex) { marked[vertex] = true; Console.WriteLine(vertex); ArrayList adjacent = g.Adjacent(vertex); for (int i = 0; i < adjacent.Count; i++) { int currentVertex = (int) adjacent[i]; if (!marked[currentVertex]) { dfs(g, currentVertex); } } }
public Frontier(DirectedGraph <TData> graph) { _graph = graph; _vertices = new HashSet <Vertex <TData, int> >(); _edges = new HashSet <Edge <TData, int> >(); }
public EularianPath(DirectedGraph <T> directedGraph) { _directedGraph = directedGraph; }
public DirectedGraphDepthFirst(DirectedGraph g, int vertex) { marked = new bool[g.getTotalVertices()]; dfs(g, vertex); }
public TopologicalOrdering(DirectedGraph <T> directedGraph) { _directedGraph = directedGraph; _ordering = new List <T>(_directedGraph.Nodes.Count); }
static void ex41() { GraphNode <int> n1 = new GraphNode <int>() { Value = 1 }; GraphNode <int> n2 = new GraphNode <int>() { Value = 2 }; n2.Adjacents.Add(n1); n1.Adjacents.Add(n2); GraphNode <int> n3 = new GraphNode <int>() { Value = 1 }; GraphNode <int> n4 = new GraphNode <int>() { Value = 1 }; GraphNode <int> n5 = new GraphNode <int>() { Value = 1 }; GraphNode <int> n6 = new GraphNode <int>() { Value = 1 }; GraphNode <int> n7 = new GraphNode <int>() { Value = 1 }; GraphNode <int> n8 = new GraphNode <int>() { Value = 1 }; n3.Adjacents.Add(n2); n2.Adjacents.Add(n3); n4.Adjacents.Add(n2); n3.Adjacents.Add(n4); n3.Adjacents.Add(n5); n4.Adjacents.Add(n3); n5.Adjacents.Add(n2); n5.Adjacents.Add(n6); n6.Adjacents.Add(n4); n6.Adjacents.Add(n7); n7.Adjacents.Add(n5); n7.Adjacents.Add(n8); n8.Adjacents.Add(n2); n8.Adjacents.Add(n6); DirectedGraph <int> graph = new DirectedGraph <int>(); graph.Nodes.Add(n1); graph.Nodes.Add(n2); graph.Nodes.Add(n3); graph.Nodes.Add(n4); graph.Nodes.Add(n5); graph.Nodes.Add(n6); graph.Nodes.Add(n7); graph.Nodes.Add(n8); ex_4_1 ex = new ex_4_1(); Console.Out.WriteLine($"{ex.IsConnected(graph, n1, n8)}"); }
private int[] pathMatrix; //previous edge in shortest path public DirectedGraphBreadthFirst(DirectedGraph g) { marked = new bool[g.getTotalVertices()]; distanceMatrix = new int[g.getTotalVertices()]; pathMatrix = new int[g.getTotalVertices()]; }
//for directed Graph public static IReadOnlyDictionary <GraphNode <T>, GraphNode <T> > RunDijkstraFrom(DirectedGraph <T> graph, T src) { if (!graph.Nodes.TryGetValue(new GraphNode <T>(src), out var source)) { return(null); } var predecessors = new Dictionary <GraphNode <T>, GraphNode <T> >();//keeps track of the shortest path predecessor //using Dijkstra's algorithm var priorityQ = new MinHeap <GraphNode <T> >(new GraphNode <T>(default(T))); foreach (var node in graph.Nodes) { node.Weight = source.Equals(node) ? 0 : int.MaxValue; priorityQ.Add(node); predecessors[source] = null; } while (priorityQ.Count != 0) { var minNode = priorityQ.GetMin(); if (!graph.Neighbors.ContainsKey(minNode)) { priorityQ.ExtractMin(); continue; } foreach (var neighbor in graph.Neighbors[minNode]) { graph.Nodes.TryGetValue(neighbor, out var neighborNode); var edges = graph.GetEdges(minNode, neighbor); foreach (var edge in edges) { if (neighborNode.Weight > minNode.Weight + edge.Weight) { neighborNode.Weight = minNode.Weight + edge.Weight; predecessors[neighborNode] = minNode; } } } priorityQ.ExtractMin(); } return(predecessors); }