Esempio n. 1
0
 public BreadthFirstPaths(Graph graph, int sourceVertex)
 {
     _sourceVertex = sourceVertex;
     _marked = new bool[graph.VertexCount];
     _edgeTo = new int[graph.VertexCount];
     Bfs(graph, sourceVertex);
 }
Esempio n. 2
0
 public DepthFirstPaths(Graph graph, int sourceVertex)
 {
     _sourceVertex = sourceVertex;
     _marked = new bool[graph.VertexCount];
     _edgeTo = new int[graph.VertexCount];
     DepthFirstSearch(graph, sourceVertex);
 }
Esempio n. 3
0
 private static Graph Create(string filePath)
 {
     int vertexCount = int.Parse(File.ReadLines(filePath).First());
     var result = new Graph(vertexCount);
     foreach (string line in File.ReadLines(filePath).Skip(2))
     {
         string[] vertexes = line.Split(null);
         result.AddEdge(int.Parse(vertexes[0]), int.Parse(vertexes[1]));
     }
     return result;
 }
Esempio n. 4
0
 private void DepthFirstSearch(Graph graph, int vertex)
 {
     _marked[vertex] = true;
     foreach (int w in graph.Adjacent(vertex))
     {
         if (_marked[w])
         {
             continue;
         }
         _edgeTo[w] = vertex;
         DepthFirstSearch(graph, w);
     }
 }
Esempio n. 5
0
 private void Bfs(Graph graph, int vertex)
 {
     _marked[vertex] = true;
     var queue = new Queue<int>();
     queue.Enqueue(vertex);
     while (queue.Count != 0)
     {
         vertex = queue.Dequeue();
         foreach (int adjacentVertex in graph.Adjacent(vertex))
         {
             _marked[adjacentVertex] = true;
             _edgeTo[adjacentVertex] = vertex;
             queue.Enqueue(adjacentVertex);
         }
     }
 }