Files
CypherCore/Source/Framework/Algorithms/DepthFirstSearch.cs
T
2023-01-18 17:25:32 -05:00

65 lines
2.0 KiB
C#

// Copyright (c) CypherCore <http://github.com/CypherCore> All rights reserved.
// Licensed under the GNU GENERAL PUBLIC LICENSE. See LICENSE file in the project root for full license information.
using System;
using Framework.Collections;
namespace Framework.Algorithms
{
public class DepthFirstSearch
{
private bool[] marked; // marked[v] = is there an s-v path?
private int count; // number of vertices connected to s
/**
* Computes the vertices in graph {@code G} that are
* connected to the source vertex {@code s}.
* @param G the graph
* @param s the source vertex
* @throws IllegalArgumentException unless {@code 0 <= s < V}
*/
public DepthFirstSearch(EdgeWeightedDigraph G, uint s, Action<uint> action)
{
marked = new bool[G.NumberOfVertices];
//validateVertex(s);
dfs(G, s, action);
}
// depth first search from v
private void dfs(EdgeWeightedDigraph G, uint v, Action<uint> action)
{
count++;
marked[v] = true;
foreach (var w in G.Adjacent((int)v))
{
if (!marked[w.To])
{
action(w.To);
dfs(G, w.To, action);
}
}
}
/**
* Is there a path between the source vertex {@code s} and vertex {@code v}?
* @param v the vertex
* @return {@code true} if there is a path, {@code false} otherwise
* @throws IllegalArgumentException unless {@code 0 <= v < V}
*/
public bool Marked(int v)
{
//validateVertex(v);
return marked[v];
}
/**
* Returns the number of vertices connected to the source vertex {@code s}.
* @return the number of vertices connected to the source vertex {@code s}
*/
public int Count()
{
return count;
}
}
}