A simple Graph Search Algorithm

Source: Internet
Author: User

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 );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.