求Graph任意两点之间的所有路径
穷举图中任意两点之间的所有路径,该问题也很有应用价值。
Code
1 /**//// <summary>
2 /// pcd图的相关算法
3 /// </summary>
4 public class GraphAlgorithms_PCD<T>
5 {
6 任意两点之间的所有路径#region 任意两点之间的所有路径
7 /**//// <summary>
8 /// 起点
9 /// </summary>
10 private Vertex<T> StartV;
11 /**//// <summary>
12 /// 终点
13 /// </summary>
14 private Vertex<T> EndV;
15 /**//// <summary>
16 /// 路径数量
17 /// </summary>
18 private int LUCount = 0;
19 /**//// <summary>
20 /// 所有的路径
21 /// </summary>
22 private Dictionary<int, List<Vertex<T>>> ALL_LJ = new Dictionary<int, List<Vertex<T>>>();
23 /**//// <summary>
24 /// 当前队列
25 /// </summary>
26 private static Stack<Vertex<T>> myStack = new Stack<Vertex<T>>();
27 /**//// <summary>
28 /// 任意两点之间的所有路径(返回结果为从结束点到起点的倒序序列)
29 /// </summary>
30 /// <param name="g">图</param>
31 /// <param name="startV">起点</param>
32 /// <param name="endV">终点</param>
33 /// <returns></returns>
34 public Dictionary<int, List<Vertex<T>>> GetAllPathBetweenTwoVertex(Graph<T> g, Vertex<T> startV, Vertex<T> endV)
35 {
36 StartV = startV;
37 EndV = endV;
38 LUCount = 0;
39 ALL_LJ.Clear();
40 myStack.Clear();
41
42 myStack.Push(StartV);
43 for (int i = 0; i < StartV.EmanatingDegree; i++)
44 {
45 Vertex<T> vertex = StartV.EmanatingEdges[i].GetPartnerVertex(StartV);
46 myStack.Push(vertex);
47 search(g, vertex);
48 }
49 return ALL_LJ;
50 }
51 private void search(Graph<T> SG, Vertex<T> V)
52 {
53 if (V == EndV)
54 {
55 //打印路径
56 List<Vertex<T>> res = new List<Vertex<T>>();
57 foreach (Vertex<T> v in myStack)
58 {
59 res.Add(v);
60 }
61 ALL_LJ.Add(LUCount, res);
62 myStack.Pop();
63 LUCount++;
64 return;
65 }
66 for (int i = 0; i < V.EmanatingDegree; i++)
67 {
68 Vertex<T> vertex = V.EmanatingEdges[i].GetPartnerVertex(V);
69 myStack.Push(vertex);
70 search(SG, vertex);
71
72 }
73 myStack.Pop();
74
75 }
76 #endregion
77 }
1 /**//// <summary>
2 /// pcd图的相关算法
3 /// </summary>
4 public class GraphAlgorithms_PCD<T>
5 {
6 任意两点之间的所有路径#region 任意两点之间的所有路径
7 /**//// <summary>
8 /// 起点
9 /// </summary>
10 private Vertex<T> StartV;
11 /**//// <summary>
12 /// 终点
13 /// </summary>
14 private Vertex<T> EndV;
15 /**//// <summary>
16 /// 路径数量
17 /// </summary>
18 private int LUCount = 0;
19 /**//// <summary>
20 /// 所有的路径
21 /// </summary>
22 private Dictionary<int, List<Vertex<T>>> ALL_LJ = new Dictionary<int, List<Vertex<T>>>();
23 /**//// <summary>
24 /// 当前队列
25 /// </summary>
26 private static Stack<Vertex<T>> myStack = new Stack<Vertex<T>>();
27 /**//// <summary>
28 /// 任意两点之间的所有路径(返回结果为从结束点到起点的倒序序列)
29 /// </summary>
30 /// <param name="g">图</param>
31 /// <param name="startV">起点</param>
32 /// <param name="endV">终点</param>
33 /// <returns></returns>
34 public Dictionary<int, List<Vertex<T>>> GetAllPathBetweenTwoVertex(Graph<T> g, Vertex<T> startV, Vertex<T> endV)
35 {
36 StartV = startV;
37 EndV = endV;
38 LUCount = 0;
39 ALL_LJ.Clear();
40 myStack.Clear();
41
42 myStack.Push(StartV);
43 for (int i = 0; i < StartV.EmanatingDegree; i++)
44 {
45 Vertex<T> vertex = StartV.EmanatingEdges[i].GetPartnerVertex(StartV);
46 myStack.Push(vertex);
47 search(g, vertex);
48 }
49 return ALL_LJ;
50 }
51 private void search(Graph<T> SG, Vertex<T> V)
52 {
53 if (V == EndV)
54 {
55 //打印路径
56 List<Vertex<T>> res = new List<Vertex<T>>();
57 foreach (Vertex<T> v in myStack)
58 {
59 res.Add(v);
60 }
61 ALL_LJ.Add(LUCount, res);
62 myStack.Pop();
63 LUCount++;
64 return;
65 }
66 for (int i = 0; i < V.EmanatingDegree; i++)
67 {
68 Vertex<T> vertex = V.EmanatingEdges[i].GetPartnerVertex(V);
69 myStack.Push(vertex);
70 search(SG, vertex);
71
72 }
73 myStack.Pop();
74
75 }
76 #endregion
77 }