strange thing but realized it…, shows shortest ways from source to other points, using BFS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SymbolGraphExample { class DegreesOfSeparation { String filepath { get; set; } char delimiter { get; set; } String source { get; set; } SymbolGraph sg; Graph G; public DegreesOfSeparation(string afilename, char adelimiter, string asource) { filepath = afilename; delimiter = adelimiter; source = asource; sg = new SymbolGraph(filepath, delimiter); G = sg.getGraph(); } public void Execute() { if (!sg.contains(source)) { Console.WriteLine(source + " not in database."); return; } int s = sg.indexOf(source); BreadthFirstPaths bfs = new BreadthFirstPaths(G, s); using (StreamReader sr = new StreamReader(filepath)) { Queue<string> alreadyProcessed = new Queue<string>(); while (sr.Peek() >= 0) { String sink = sr.ReadLine(); String[] substrings = sink.Split(delimiter); foreach (string substring in substrings) { if (sg.contains(substring)) { int t = sg.indexOf(substring); if (bfs.hasPathTo(t)&&(substring!=source)&&(!alreadyProcessed.Contains(substring))) { Console.WriteLine(""); Console.Write("path to "+substring+" : "); alreadyProcessed.Enqueue(substring); foreach (int v in bfs.pathTo(t)) { Console.Write(" " + sg.nameOf(v)); } } } } } } } } } |