greedy algorithm (also known as greedy algorithm greedy): when solving problems, always make the best choice at present. is only the local optimal solution in some sense. The greedy algorithm can not get the whole optimal solution for all the problems, but it can produce the whole optimal solution or the approximate solution of the whole optimal solution for many problems with wide range.
There are two important properties of the problem that can be solved by greedy algorithm:
1. Greedy Choice Nature
In the current state to make the best choice, that is, the local optimal selection, and then to solve the problem of the response generated after the choice, usually in a top-down manner, in an iterative way to make successive greedy choice, each time the greedy choice will be asked to simplify the problem to a smaller size sub-problem. In the dynamic programming algorithm, the choice of each step often depends on the solution of the related sub-problem (bottom-up).
2. Optimal substructure Properties
When the optimal solution of a problem contains the optimal solution of its sub-problem, it is said that this problem has the optimal substructure property.
In this case, the knapsack problem, similar to the 0-1 knapsack problem, the difference is that the choice item I in the backpack, you can choose Part I rather than all. This knapsack problem can be solved by greedy algorithm: the item unit weight value from high to low order into the backpack, until the backpack is filled. For the 0-1 knapsack problem, the greedy choice is unable to get the best solution because in this case, it is not guaranteed to eventually fill the backpack, part of the empty backpack space to reduce the value of each kilogram of backpack space.
Single source shortest path greedy algorithm implementation:
Public class maxsum {static int max_size=6;public static void dijkstra (Int v,float[][]a,float[]dist,int[]prev) { int n=dist.length-1; if (v<1| | v>n) return; boolean []s=new boolean[n+1]; for (int i=1;i<=n;i++) { dist[i]=a[v][i]; s[i]= False; if (Dist[i]==float.max_value) prev[i]=0; else prev[i]=v; } dist[v]=0;s[v]= True; for (int i=1;i<n;i++) { float temp= Float.max_value; int u=v; for (int j=1;j<=n;j++) if (!s[j) && (dist[j]<temp) { u=j; temp=dist[j]; } s[u]=true; for (int j=1;j<=n;j++) if (!s[j) && (a[u][j]<float.max_value) { float newdist=dist[u]+a[u][j]; if (Newdist<dist[j]) { dist[j]=newdist; prev[j]=u; } } }}public static void main (string args[]) { float a[ ][]=new float[max_size][max_size];float[]dist=new float[max_size];int []prev=new int[max_ Size]; for (int i=0;i<6;i++) for (int j=0;j<6;j++) &NBSP;&NBSP;&NBSP;&NBSP;A[I][J]=float.max_value; a[1][2]=10; a[1][4]=30; a[1][5]=100; a[2][3]=50; a[3][5]=10; a[4][3]=20; a[4][ 5]=60; int v=1;//assumes a departure from Vertex 1 dijkstra (V,a,dist,prev); system.out.println ("Starting from 1 to 2, 3, 4, 5 The shortest path is:"); for (int j=2;j<6; J + +) { system.out.println (Dist[j]); } int z=prev[5],y=prev[z],x=prev[y]; system.out.println ("The point from 1 to 5 shortest path is:" ); system.out.print (x+ " " +y+ " " +z+ " " + "5");}}
Operation Result:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/54/A2/wKiom1SICiCB7PsNAABvc3wsGxg096.jpg "title=" Untitled. png "alt=" wkiom1sicicb7psnaabvc3wsgxg096.jpg "/>
650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0003.gif "alt=" J_0003.gif "/>
This article is from a "stroll," blog, please be sure to keep this source http://macxiao.blog.51cto.com/9606147/1588385
Dijkstra single Source Shortest path (greedy selection)