Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
BFS ALGO
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { class Program { public class Employee { public Employee(string name) { this.name = name; } public string name { get; set; } public List<Employee> Employees { get { return EmployeesList; } } public void isEmployeeOf(Employee p) { EmployeesList.Add(p); } List<Employee> EmployeesList = new List<Employee>(); public override string ToString() { return name; } } public class BreadthFirstAlgorithm { public Employee BuildEmployeeGraph() { Employee Eva = new Employee("Eva"); Employee Sophia = new Employee("Sophia"); Employee Brian = new Employee("Brian"); Eva.isEmployeeOf(Sophia); Eva.isEmployeeOf(Brian); Employee Lisa = new Employee("Lisa"); Employee Tina = new Employee("Tina"); Employee John = new Employee("John"); Employee Mike = new Employee("Mike"); Sophia.isEmployeeOf(Lisa); Sophia.isEmployeeOf(John); Brian.isEmployeeOf(Tina); Brian.isEmployeeOf(Mike); return Eva; } public Employee Search(Employee root, string nameToSearchFor) { Queue<Employee> Q = new Queue<Employee>(); HashSet<Employee> S = new HashSet<Employee>(); Q.Enqueue(root); S.Add(root); while (Q.Count > 0) { Employee e = Q.Dequeue(); if (e.name == nameToSearchFor) return e; foreach (Employee friend in e.Employees) { if (!S.Contains(friend)) { Q.Enqueue(friend); S.Add(friend); } } } return null; } public void Traverse(Employee root) { Queue<Employee> traverseOrder = new Queue<Employee>(); Queue<Employee> Q = new Queue<Employee>(); HashSet<Employee> S = new HashSet<Employee>(); Q.Enqueue(root); S.Add(root); while (Q.Count > 0) { Employee e = Q.Dequeue(); traverseOrder.Enqueue(e); foreach (Employee emp in e.Employees) { if (!S.Contains(emp)) { Q.Enqueue(emp); S.Add(emp); } } } while (traverseOrder.Count > 0) { Employee e = traverseOrder.Dequeue(); Console.WriteLine(e); } } } static void Main(string[] args) { BreadthFirstAlgorithm b = new BreadthFirstAlgorithm(); Employee root = b.BuildEmployeeGraph(); Console.WriteLine("Traverse Graph\n------"); b.Traverse(root); Console.WriteLine("\nSearch in Graph\n------"); Employee e = b.Search(root, "Eva"); Console.WriteLine(e == null ? "Employee not found" : e.name); e = b.Search(root, "Brian"); Console.WriteLine(e == null ? "Employee not found" : e.name); e = b.Search(root, "Soni"); Console.WriteLine(e == null ? "Employee not found" : e.name); } } } } } }
Show compiler warnings
[
+
]
Show input
edit mode
|
history
|
discussion