FIG representation: the adjacency list, adjacency matrix
Type: directed graph, an undirected graph
Edge penetration, the degree of node values, the connection node connections: node information
package day7; import java.util.ArrayList; public class Node { public int value; public int in; public int out; public ArrayList<Node> next; //这里是list public ArrayList<Edge> edge; public Node(int value) { this.value = value; this.in = 0; this.out = 0; this.next = new ArrayList<>(); this.edge = new ArrayList<>(); } }
Side information: edge weights, from a node, to the node
package day7; public class Edge { public int value; public Node from; public Node to; public Edge(int value,Node from ,Node to) { this.value = value; this.from = from; this.to = to; } }
FIG: a collection of nodes set of edges +
initialization:
package day7; import java.util.HashMap; import java.util.HashSet; public class Graph { public HashMap<Integer, Node> nodes ; public HashSet<Edge> edges; public Graph() { this.nodes = new HashMap<Integer, Node>(); this.edges = new HashSet<Edge>(); } public void creatGraph(int arr[][]) { for (int i = 0;i <arr.length ; i++) {//arr[i] ={from,to,value} Node from,to; if(!this.nodes.containsKey(arr[i][0])) { from = new Node(arr[i][0]); }else { from = this.nodes.get(arr[i][0]); } from.out++; if(!this.nodes.containsKey(arr[i][1])) { to = new Node(arr[i][1]); }else { to = this.nodes.get(arr[i][1]); }; to.in ++; from.next.add(to); Edge edge = new Edge(arr[i][2], from, to); from.edge.add(edge); to.edge.add(edge); this.nodes.put(arr[i][0], from); this.nodes.put(arr[i][1], to); this.edges.add(edge); } } }
FIG width-first traversal BDF
- Queue implementation
- Starts from a node, according to wide sequentially into the queue, then the pop-up
- After the pop, look no been to the critical point of the queue into the queue
- Until the queue is empty
package day7; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; public class Code01_BFS { public static void bfs(Node node) { if(node == null) return ; //这里要用LinkList 而不是 PriorityQueue Queue <Node> que = new LinkedList<Node>(); HashSet<Node> set = new HashSet<Node>(); que.add(node); set.add(node); while(!que.isEmpty()) { Node cur = que.poll(); System.out.print(cur.value+" "); for (Node next : cur.next) { if(!set.contains(next)) { que.add(next); set.add(next); } } } } public static void main(String[] args) { // int arr[][] = { // {1,2,7,}, // {1,3,5,}, // {2,3,2,},}; int arr [][]= {{1,2,1},{1,3,2},{2,4,3},{2,5,2},{4,6,4},}; Graph graph = new Graph(); graph.creatGraph(arr); bfs(graph.nodes.get(1)); } } /* * * * */
FIG depth first traversal BDF
- Stack implementation
- From a start node, in accordance with the depth of the stack in turn, and then pop
- After the pop-up current node, the contrast of a node adjacent to the point not been to stack, the stack. (The current node also stack)
- To stack empty
package day7; import java.util.HashSet; import java.util.Stack; public class Code02_DFS { public static void dfs (Node node) { //每次都忘了 if (node == null) return ; HashSet<Node> set = new HashSet<Node>(); Stack <Node> stack = new Stack<>(); stack.add(node); set.add(node); while(!stack.isEmpty()) { Node cur = stack.pop(); System.out.print(cur.value+ " "); for (Node next : cur.next) { if (! set.contains(next)) { stack.add(next); set.add(next); cur = next; } } } } public static void main(String[] args) { int arr [][]= { {1,2,1},{1,3,2},{2,4,3},{2,5,2},{4,6,4},}; // int arr [][]= { // {1,2,1},{1,3,2},{1,4,2}, // {2,3,1},{2,4,3},{3,4,4},}; Graph graph = new Graph(); graph.creatGraph(arr); dfs(graph.nodes.get(1)); } }
Topological sorting:
Requirements: the presence of the 0-degree nodes of a directed graph , and no ring . (Condition 3)
Package Day7; Import of java.util.ArrayList; Import the java.util.HashMap; Import java.util.LinkedList; Import java.util.List; Import java.util.Queue; public class Code03_TopologicalSort { public static List <the Node> topologicalSort ( Graph Graph) { // here the Map should be used, with the changed set of information into the node, a set secret add. map with PUT // HashSet <the Node> SET = new new HashSet <the Node> (); the HashMap <the Node, Integer> = inMap new new the HashMap <the Node, Integer> (); Queue <the Node> Queue = new new LinkedList<Node>(); for (Node node: graph.nodes.values()) { inMap.put(node,node.in); if (node.in == 0) { queue.add(node); } } List <Node> result = new ArrayList<Node>(); while (!queue.isEmpty()) { Node cur = queue.poll(); result.add(cur); System.out.print(cur.value+" "); for (Node next : cur.next) { inMap.put(next,inMap.get(next) -1 ); if(inMap.get(next) == 0) { queue.add(next); } } } return result; } public static void main(String[] args) { int arr [][]= {{1,2,1},{1,3,2},{3,2,3},{2,4,3},{2,5,2},{3,5,8},{4,5,3},{4,6,4},}; Graph graph = new Graph(); graph.creatGraph(arr); topologicalSort(graph); } }
Minimum Spanning Tree : case guaranteed communication, the edge weights and a minimum set of! ! ! ! ! ! ! Function returns a set of edges! ! ! ! ! ! !
Kruskal:
Selected from the side, from small to large (priority queue), the circuit is not formed (disjoint-set) until all the points are traversed all sides comprise end ||
Package Day7; Import java.util.Collection; Import java.util.Comparator; Import the java.util.HashMap; Import java.util.HashSet with; Import java.util.PriorityQueue; Import java.util.Queue; Import Classes in java.util. sET; Import the java.util.Stack; / * * minimum spanning tree, the set of edges returned * Kruskal: * selected from the sides, from small to large (priority queue), the circuit is not formed (disjoint-set) until all points comprising All edges are traversed end || * / public class Code05_Kruskal { // disjoint-set // two map, a father, head. public static class UnionFind { HashMap<Node, Node> fatherMap ; HashMap <Node ,Integer> sizeMap; public UnionFind() { fatherMap = new HashMap<Node, Node>(); sizeMap = new HashMap<Node, Integer>(); } public void inital (Collection<Node> nodes) { this.fatherMap.clear(); this.sizeMap.clear(); for (Node cur : nodes) { this.fatherMap.put(cur, cur); this.sizeMap.put(cur, 1); } } public boolean isSameSet(Node a, Node b) { return this.findFather1(a) == this.findFather1(b); } public void union(Node a, Node b) { Node fathera = findFather1(a); Node fatherb = findFather1(b); int sizea = this.sizeMap.get(fathera); int sizeb = this.sizeMap.get(fatherb); if (sizea <sizeb) { this.fatherMap.put(fathera, fatherb); this.sizeMap.put(fatherb, sizea +sizeb); }else { this.fatherMap.put(fatherb, fathera); this.sizeMap.put(fathera,sizea +sizeb ); } } //递归版本 public Node findFather2(Node cur) { if (!this.fatherMap.containsKey(cur)) return null; //get之前一定要记得判断null Node father = fatherMap.get(cur); while(! = Father CUR) { Father = findFather2 (Father); } the this .fatherMap.put (CUR, Father); return Father; } // nonrecursive version public the Node findFather1 (the Node CUR) { IF ! ( the this .fatherMap. containsKey (CUR)) return null ; // before determination must remember to get null the Node Father = fatherMap.get (CUR); Stack <the Node> Stack = new new Stack <the Node> (); the while ! (CUR = Father) { stack.add(cur); cur = father; father = fatherMap.get(cur); } while (!stack.isEmpty()) { this.fatherMap.put(stack.pop(), father); } return father; } } public static class MyComaprator implements Comparator<Edge>{ @Override public int compare(Edge o1, Edge o2) { return o1.value -o2.value; } } public static Set<Edge> kruskal (Graph graph) { Queue<Edge> queue = new PriorityQueue<Edge>(new MyComaprator()); UnionFind union = new UnionFind(); union.inital(graph.nodes.values()); for (Edge edge : graph.edges) { queue.add(edge); } Set<Edge> result = new HashSet<Edge>(); while(!queue.isEmpty()) { Edge edge = queue.poll(); if(!union.isSameSet(edge.from, edge.to)) { result.add(edge); System.out.print(edge.value + " "); union.union(edge.from, edge.to); } } return result; } public static void main(String[] args) { int arr [][]= { {1,2,6},{1,3,1},{1,4,5},{2,3,5},{3,4,5}, {2,5,3},{3,5,6},{3,6,4},{4,6,2},{5,6,6}, // {2,1,6},{3,1,1},{4,1,5},{3,2,5},{4,3,5}, // {5,2,3}, {5,3,6}, {6,3,4}, {6, 4}, {6,5,6}, }; // undirected graph 153 42 // digraph 15,342. The default undirected graph. Because the edge is selected, regardless of the direction // int ARR [] [] = { // {1,2,1}, {1,3,2}, {2,4,3}, {2,5,2 }, {4,6,4},}; Graph Graph = new new Graph (); graph.creatGraph (ARR); Kruskal (Graph); } }
Prim:
- Starting from the node to find adjacent sides,
- Select the smallest edge, find nodes connected by an edge if there have been,
- Does not appear, while adding the result set, the new node unlocked (unlock new edge), the newly added together with 2 side repeat the original edge.
Package Penalty for Day7; Import java.util.Comparator; Import java.util.HashSet; / * * Minimum spanning tree: Returns the set of edges * Prim: From the point, look for the adjacent side, choose the smallest to see the side adjacent nodes whether there had been * does not appear, add result, unlock new variants and the original side together, looking for the smallest * continue appears * * / Import java.util.PriorityQueue; Import java.util.Set; public class Code04_Prim { public static class MyComparator the implements Comparator <Edge> { @Override public int Compare (the arg0 Edge, Edge arg1) { return arg0.value - arg1.value; } } public static Set<Edge> prim (Graph graph){ HashSet<Node> set = new HashSet<Node> (); PriorityQueue<Edge> queue = new PriorityQueue<Edge>(new MyComparator()); Set<Edge> result = new HashSet<Edge>(); //important for (Node node : graph.nodes.values()) { if (!set.contains(node)) { set.add(node); for (Edge edge : node.edge) { queue.add(edge); } while (!queue.isEmpty()) { Edge edge = queue.poll(); Node toNode = edge.to; if(!set.contains(toNode)) { set.add(toNode); //添加新点,从新点出发。 result.add(edge); System.out.println("from " +edge.from.value+" to "+edge.to.value+" vlaue "+ edge.value ); for (Edge nextedge : toNode.edge) { queue.add(nextedge); } } } } } Return Result; } public static void main (String [] args) { int ARR [] [] = { { l, 2,6}, {1,3,1}, {1,4,5} , {2,3,5}, {3,4,5 }, { 2,5,3}, {3,5,6}, {3,6,4}, {4,6,2}, { 5,6,6 }, // {2,1,6}, {3,1,1}, {4,1,5}, {3,2,5}, {4,3,5}, / / {5,2,3}, {5,3,6}, {6,3,4}, {6, 4}, {6,5,6}, }; // no 15 to FIG. 342 // digraph 14563 // int arr [][]= { // {1,2,1},{1,3,2},{2,4,3},{2,5,2},{4,6,4},}; Graph graph = new Graph(); graph.creatGraph(arr); prim(graph); } }