To find the shortest path of two nodes, for unauthorized graphs, you can through the breadth of the first traversal solution. The weighted graph is usually solved by Dijkstra algorithm.
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map; public class Shortest {static class cell{int node;//The weights public Cell (int node,int weight) connected to which node int weight;//Edge {This.nod
E=node;
This.weight=weight; @SuppressWarnings ("unchecked") public static void main (string[] args) {list[] g=new list[11]; for (int I=0;i<g.leng
th;i++) g[i]=new ArrayList ();
adjacency Table Form G[0].add (new Cell (1,3));
G[0].add (New Cell (4,1));
G[1].add (New Cell (2,1));
G[1].add (New Cell (6,3));
G[1].add (New Cell (9,4));
G[1].add (New Cell (5,5));
G[1].add (New Cell (0,3));
G[2].add (New Cell (1,1));
G[2].add (New Cell (3,1));
G[2].add (New Cell (6,7));
G[3].add (New Cell (2,1));
G[3].add (New Cell (10,2));
G[4].add (new Cell (0,1));
G[4].add (New Cell (5,2));
G[5].add (New Cell (4,2));
G[5].add (new Cell (1,5));
G[5].add (New Cell (7,2));
G[5].add (New Cell (8,3));
G[6].add (new Cell (2,3));
G[6].add (New Cell (3,7));
G[6].add (New Cell (8,2));
G[6].add (New Cell (10,1)); G[7].add (New Cell (5,2));
G[8].add (New Cell (5,3));
G[8].add (New Cell (6,2));
G[9].add (New Cell (1,4));
G[9].add (New Cell (10,2));
G[10].add (New Cell (3,2));
G[10].add (New Cell (6,1));
G[10].add (New Cell (9,2));
Find all the least path Map map=new HashMap () Starting with node No. 0; while (true) {int min=integer.max_value;//min path value int min_no=-1;//corresponding node number//All connections to node No. 0 and not in map for (int i=0;i<g[0].size (
i++) {cell t= (cell) g[0].get (i); if (Map.get (T.node) ==null&&t.weight<min) {min_no=t.node; min=t.weight;}}
Adjacent to the map midpoint, all nodes that are not in the map (the distance that may experience multiple points below and the point directly adjacent to it) iterator It=map.keyset (). iterator (); while (It.hasnext ()) {int k= (integer) it.next (); int w= (integer) map.get (k);//The node in the collection corresponds to the minimum path value for (int i=0;i<g[k].size (); i++) {cell t= (cell) g[k].get (i); if (Map.get (T.node) ==null&&t.weight+w<min) {min_no=t.node; min=
T.weight+w;
}} if (Min<integer.max_value) {map.put (min_no,min);} else{break;}
System.out.print (map); }
}
Results: {0=2, 1=3, 2=4, 3=5, 4=1, 5=3, 6=6, 7=5, 8=6, 9=7, 10=7} 0 to itself the distance calculated is according to 4, only from 4 back to 0, so equals 2
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
The so-called "traversal" or "enumeration" is to list all the circumstances. The main point is to meet two requirements: 1 cannot be duplicated; 2 cannot be omitted. "Cannot repeat" requires us to have a methodical approach in the traversal, followed by a design route. Test and backtracking are the most common and easy to understand design ideas.
Eight Queens problem has multiple solutions
public class Noattack {
/**
* Eight queens problem, here does not need to use 8*8 chessboard, must each queen not the same line, can attack at the same time cannot be in the diagonal position, the attack distance unlimited
* * *
/**
* Check whether the new queen has been put in conflict/
static Boolean check (int[] A, int row, int col) {for
(int i = 0; i < row; i++) {
// Whether there is a conflict in portrait if
(col = = A[i]) return
false;//column conflict with previous Queen
//Diagonal Check
if (row-i = = Math.Abs (col-a[i)))
return false;
return true;
}
static void Show (int[] a) {for
(int i=0;i<a.length;i++) {
System.out.print (a[i]+ "");
}
System.out.println ();
}
/**
* Sets the K Queen */
static void F (int[] A, int k) {
if (k = = 8) {Show
(a);
return;//out Recursive
}
//test for 8 locations
(int i = 0; i < 8; i++) {
a[k] = i;
Place the K queen in position I and check
if (check (a, k, i))
f (A; k + 1);
}
public static void Main (string[] args) {
int[] a = new int[8];//records the position of the Queen in each row
F (A, 0);
}