Maybe in my life, I often encounter a problem. Under many restrictions, how can I find an optimal solution? You may have come up with a lot of ideas, such as "linear planning" and "dynamic planning"
These classic strategies, of course, have some problems. We can use greed to find the overall optimal solution. In graph theory, a typical example of greedy method to find the optimal solution is simply the problem of "Shortest Path.
I. Overview
I want to find the shortest path from V0 to V3. you will find that there are many two paths to them: V0-> V4-> V3, V0-> V1-> V3, of course, you will think that the former is the shortest you are looking.
Path. If there are so many vertices in the graph, will you find them so easily? Next we will sort out our greedy thinking system.
Ii. Construction
If you already know primAlgorithmThen, the Dijkstra algorithm only extends above it, which is actually very simple.
1. Edge Nodes
The difference here is that I define a vertexs on the edge to record the nodes that I used to search for when a node was greedy. For example, when we searched for V3 from V0 greedy, We v3
Vertexs may store v0, V4, and V3, which were previously passed by. Maybe the last three nodes are the shortest path we are looking.
1 # region edge information 2 // <summary> 3 // edge information 4 /// </Summary> 5 public class edge 6 {7 // start edge 8 public int startedge; 9 10 // end side 11 Public int endedge; 12 13 // weight 14 public int weight; 15 16 // whether to use 17 Public bool isuse; 18 19 // accumulate vertex 20 public hashset <int> vertexs = new hashset <int> (); 21} 22 # endregion
2. Dijkstra Algorithm
First, we will analyze the steps of the Dijkstra algorithm:
There are five elements including M = {v0, V1, V2, V3, V4} in the set. We use
Tempvertex indicates whether the vertex is used.
Weight indicates the weight of the path (maxvalue is used by default ).
Path indicates the total weight of the vertex.
①. Select vertex V0 from set m as the starting point. Assign values to all adjacent contacts of v0, on the premise that the weight value must be smaller than the original weight value, and exclude that it has been accessed
And then select the smallest weight as the starting point for the next greedy search. In this way, v0v1 selects the shortest path, 2.
②. We continue to assign values to the adjacent contacts starting from the vertex V1 in the same way. Finally, we find that v0v4 is the shortest path. That is, figure 3.
...
3. The shortest path of all vertices is obtained in this way.
1 # region Dijkstra algorithm 2 // <summary> 3 // Dijkstra algorithm 4 // </Summary> 5 Public dictionary <int, edge> Dijkstra () 6 {7 // collect the adjacent edge 8 Dictionary of the vertex <int, edge> dic_edges = new dictionary <int, edge> (); 9 10 // Weight = maxvalue: no side 11 for (INT I = 0; I <graph. vertexsnum; I ++) 12 {13 // start edge 14 var startedge = I; 15 16 dic_edges.add (startedge, new edge () {Weight = int. maxvalue}); 17} 18 19 // obtain the first vertex 20 var start = 0; 21 22 for (INT I = 0; I <graph. vertexsnum; I ++) 23 {24 // mark that the vertex has used 25 dic_edges [start]. isuse = true; 26 27 for (Int J = 1; j <graph. vertexsnum; j ++) 28 {29 var end = J; 30 31 // obtain the weight of the adjacent edge 32 var Weight = graph. edges [start, end]; 33 34 // assign a smaller weight of 35 if (weight <dic_edges [end]. weight) 36 {37 // sum up the weights of the previous vertex by 38 var totalweight = dic_edges [start]. weight = int. maxvalue? Weight: dic_edges [start]. weight + weight; 39 40 if (totalweight <dic_edges [end]. weight) 41 {42 // Add the adjacent edge of the vertex to the set 43 dic_edges [end] = new edge () 44 {45 startedge = start, 46 endedge = end, 47 Weight = totalweight48}; 49 50 // Add vertex of the node on the last edge to 51 dic_edges [end]. vertexs = new hashset <int> (dic_edges [start]. vertexs); 52 53 dic_edges [end]. vertexs. add (start); 54 dic_edges [end]. vertexs. add (end); 55} 56} 57} 58 59 VaR min = int. maxvalue; 60 61 // The next vertex to be compared 62 int Minkey = 0; 63 64 // obtain the minimum value of 65 foreach (var key in dic_edges.keys) in the Start edge) 66 {67 // obtain the current smallest key (except used) 68 if (min> dic_edges [Key]. weight &&! Dic_edges [Key]. isuse) 69 {70 min = dic_edges [Key]. weight; 71 Minkey = key; 72} 73} 74 75 // find 76 start = Minkey; 77} 78 79 return dic_edges; 80} 81 # endregion from the adjacent edge Vertex
TotalCode: The complexity is very bad )...
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. diagnostics; using system. threading; using system. io; using system. threading. tasks; namespace consoleapplication2 {public class program {public static void main () {dictionary <int, string> DIC = new dictionary <int, string> (); matrixgraph graph = new matrixgraph (); graph. build (); var result = graph. dijkstra (); Console. writeline ("the shortest path of each node is:"); foreach (var key in result. keys) {console. writeline ("{0}", String. join ("->", result [Key]. vertexs);} console. read ();}} # region defines a matrix node // <summary> // defines a matrix node // </Summary> public class matrixgraph {graph = New Graph (); public class graph {// <summary> // vertex information /// </Summary> Public int [] vertexs; /// <summary> /// Number of edges /// </Summary> Public int [,] Edges; /// <summary> /// Number of vertices /// </Summary> Public int vertexsnum; /// <summary> /// Number of edges /// </Summary> Public int edgesnum ;} # region matrix construction // <summary> // matrix construction // </Summary> Public void build () {// Number of vertices graph. vertexsnum = 5; // Number of edges graph. edgesnum = 6; graph. vertexs = new int [graph. vertexsnum]; graph. edges = new int [graph. vertexsnum, graph. vertexsnum]; // construct a two-dimensional array for (INT I = 0; I <graph. vertex Snum; I ++) {// vertex graph. vertexs [I] = I; for (Int J = 0; j <graph. vertexsnum; j ++) {graph. edges [I, j] = int. maxvalue ;}// defines six edges in graph. edges [0, 1] = graph. edges [1, 0] = 2; graph. edges [0, 2] = graph. edges [2, 0] = 5; graph. edges [0, 4] = graph. edges [4, 0] = 3; graph. edges [1, 3] = graph. edges [3, 1] = 4; graph. edges [2, 4] = graph. edges [4, 2] = 5; graph. edges [3, 4] = graph. edges [4, 3] = 2;} # endreg Ion # region edge information // <summary> // edge information // </Summary> public class edge {// start edge public int startedge; // end side public int endedge; // weight public int weight; // whether to use public bool isuse; // accumulative vertex public hashset <int> vertexs = new hashset <int> ();} # endregion # region Dijkstra algorithm // <summary> // Dijkstra algorithm // </Summary> Public dictionary <int, edge> Dijkstra () {// collect the adjacent edges of vertex dictionary <int, edge> dic_edges = N EW dictionary <int, edge> (); // Weight = maxvalue: identifies no edge for (INT I = 0; I <graph. vertexsnum; I ++) {// start edge var startedge = I; dic_edges.add (startedge, new edge () {Weight = int. maxvalue});} // obtain the first vertex var start = 0; For (INT I = 0; I <graph. vertexsnum; I ++) {// mark that the vertex has used dic_edges [start]. isuse = true; For (Int J = 1; j <graph. vertexsnum; j ++) {var end = J; // obtain the weight of the adjacent edge var Weight = graph. edges [start, End]; // assign a smaller weight if (weight <dic_edges [end]. weight) {// accumulate var totalweight = dic_edges [start] with the weight of the previous vertex. weight = int. maxvalue? Weight: dic_edges [start]. weight + weight; If (totalweight <dic_edges [end]. weight) {// Add the adjacent edge of the vertex to the set dic_edges [end] = new edge () {startedge = start, endedge = end, Weight = totalweight }; // accumulate the vertex of the last edge node dic_edges [end]. vertexs = new hashset <int> (dic_edges [start]. vertexs); dic_edges [end]. vertexs. add (start); dic_edges [end]. vertexs. add (end) ;}} var min = int. maxvalue; // The vertex int minke for comparison next Y = 0; // obtain the minimum value foreach (var key in dic_edges.keys) in the Start edge. {// obtain the current smallest key (except used) if (min> dic_edges [Key]. weight &&! Dic_edges [Key]. isuse) {min = dic_edges [Key]. weight; Minkey = Key ;}/// start from the vertex of the adjacent edge and find start = Minkey;} return dic_edges ;}# endregion }# endregion}