A simple Graph Search Algorithm
[Serializable]
Public class VPoint
{
Public int ID {get; set ;}
Public int X {get; set ;}
Public int Y {get; set ;}
Public bool IsFind {get; set ;}
Public List <int> Dependences {get; set;} // The vertex ID associated with the vertex
}
Private void FindPath () {VPoint v1 = new VPoint () {ID = 0, X = 0, Y = 20, Dependences = new List <int> () {2, 1 }}; VPoint v2 = new VPoint () {ID = 1, X = 20, Y = 0, Dependences = new List <int> () {0, 3, 2 }}; VPoint v3 = new VPoint () {ID = 2, X = 20, Y = 20, Dependences = new List <int> () {0, 1, 4 }}; VPoint v4 = new VPoint () {ID = 3, X = 40, Y = 0, Dependences = new List <int> () {1, 4, 5 }}; VPoint v5 = new VPoint () {ID = 4, X = 40, Y = 20, Dependences = new List <int> () {2, 3, 5 }}; VPoint v6 = new VPoint () {ID = 5, X = 60, Y = 20, Dependences = new List <int> () {3, 4 }}; list <VPoint> points = new List <VPoint> () {v1, v2, v3, v4, v5, v6}; VPoint start = v1; VPoint end = v6; list <VPoint> resultpoint = new List <VPoint> (); FindPoint (Clone (points), start, end, resultpoint); foreach (var item in Paths) {string path = ""; foreach (var point in item) {path + = point. ID + "=>";} Console. writeLine (path); // print path} Console. writeLine (count) ;}int count = 0; List <IList <VPoint> Paths = new List <IList <VPoint> (); // global variable, save all the paths private void FindPoint (List <VPoint> points, VPoint start, VPoint end, List <VPoint> resultpoint) {resultpoint. add (start); int index = points. indexOf (points. where (p => p. ID = start. ID ). toList () [0]); points [index]. isFind = true; // mark if (start. dependences. contains (end. ID) // If the destination is {resultpoint. add (end); Paths. add (resultpoint); int _ index = points. indexOf (points. where (p => p. ID = end. ID ). toList () [0]); points [_ index]. isFind = true;} else {foreach (var item in start. dependences) {var newstart = points. where (p => p. ID = item & p. isFind = false ). toList (); if (newstart. count> 0) {count ++; List <VPoint> newpoints = Clone (resultpoint); FindPoint (Clone (points), newstart [0], end, newpoints) ;}}} public static T Clone <T> (T RealObject) // deep copy {using (Stream objectStream = new MemoryStream ()) {IFormatter formatter = new BinaryFormatter (); formatter. serialize (objectStream, RealObject); objectStream. seek (0, SeekOrigin. begin); return (T) formatter. deserialize (objectStream );}}