public void Dog_Exists() { var a = new GraphNodeVis <char>('a'); a.Chain('d').Chain('o').Chain('g'); Assert.That(IsExists(a, "dog"), Is.True); }
private bool IsExists(GraphNodeVis <char> node, string word, int index = 0) { if (!word.Contains(node.Data)) { node.IsVisited = true; } if (index >= word.Length) { return(true); } if (node.Connections == null) { return(false); } foreach (var child in node.Connections.Where(x => !x.IsVisited)) { if (index == word.IndexOf(child.Data)) { if (IsExists(child, word, index + 1)) { return(true); } } else { if (IsExists(child, word, index)) { return(true); } } } return(false); }
private bool Bfs(char c, GraphNodeVis <char> node) { var queue = new Queue <GraphNodeVis <char> >(); queue.Enqueue(node); return(Bfs(c, queue)); }
public void Dog_NotExists() { var a = new GraphNodeVis <char>('a'); a.Add('b', 'r', 't'); a.Chain('m').Chain('t'); Assert.That(IsExists(a, "dog"), Is.False); }
public void Simple() { var a = new GraphNodeVis <char>('A'); a.Chain('B').Chain('D'); a.Chain('C').Chain('E'); Assert.That(Bfs('E', a), Is.True); Assert.That(nodesVisited, Is.EqualTo(5)); }
public void More_Complex() { var a = new GraphNodeVis <char>('A'); var e = new GraphNodeVis <char>('E'); a.Chain('B').Add('D').Add(e); a.Chain('C').Add(e).Add('G').Chain('F').Add('K'); Assert.That(Bfs('K', a), Is.True); Assert.That(nodesVisited, Is.EqualTo(9)); }
public GraphNodeVis <T> Chain(T data) { var node = new GraphNodeVis <T>(data); if (Connections == null) { Connections = new List <GraphNodeVis <T> >(); } Connections.Add(node); return(node); }
public void SearchTest() { var s = new GraphNodeVis <char>('S'); var b = new GraphNodeVis <char>('B'); s.Add(b); s.Add('A', 'Z', 'X'); b.Add('C'); Assert.That(Dfs(s, 'C'), Is.True); Assert.That(iterations, Is.EqualTo(3)); }
public void WithLoop() { var m = new GraphNodeVis <char>('m'); var d = new GraphNodeVis <char>('d'); var t = new GraphNodeVis <char>('t'); var o = new GraphNodeVis <char>('o'); var g = new GraphNodeVis <char>('g'); m.Add(t, d); d.Add(m, g, o, t); o.Add(d, g); g.Add(o, d); Assert.That(IsExists(m, "dog"), Is.True); }
public void Dfs_With_loop_NotFound() { var a = new GraphNodeVis <char>('A'); a.Add('D'); var t = new GraphNodeVis <char>('T'); var b = new GraphNodeVis <char>('B'); var c = new GraphNodeVis <char>('C'); a.Add(b, c); b.Add(t); c.Add(t); Assert.That(Dfs(a, 'Z'), Is.False); Assert.That(iterations, Is.EqualTo(5)); }
public bool Dfs <T>(GraphNodeVis <T> root, T data) { iterations++; root.IsVisited = true; if (root.Data.Equals(data)) { return(true); } if (root.Connections == null) { return(false); } foreach (var node in root.Connections.Where(x => !x.IsVisited)) { if (Dfs <T>(node, data)) { return(true); } } return(false); }