pull/3/head
parent
2b90643e6f
commit
5196daa576
@ -0,0 +1,71 @@
|
||||
package leo.class10_16;
|
||||
|
||||
import leo.class10_16.Graph.Node;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Leo
|
||||
* @ClassName BFS
|
||||
* @DATE 2020/12/29 2:23 下午
|
||||
* @Description 图的宽度优先遍历
|
||||
*
|
||||
*/
|
||||
public class BFS {
|
||||
|
||||
/**
|
||||
* 功能描述 : 深度优先遍历 借助set,
|
||||
* 过滤掉已经遍历过的节点
|
||||
* 出队列就打印
|
||||
* @author Leo
|
||||
* @date 2020/12/29 2:28 下午
|
||||
* @param node
|
||||
* @return void
|
||||
*/
|
||||
public static void bfs(Node node) {
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
Queue<Node> queue = new LinkedList<>();
|
||||
HashSet<Node> set = new HashSet<>();
|
||||
queue.offer(node);
|
||||
set.add(node);
|
||||
Node cur = null;
|
||||
while (!queue.isEmpty()) {
|
||||
cur = queue.poll();
|
||||
System.out.println(cur.value);
|
||||
for (Node next : cur.nexts) {
|
||||
if (!set.contains(next)) {
|
||||
set.add(next);
|
||||
queue.offer(next);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void bfs1(Node node) {
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
Queue<Node> queue = new LinkedList<>();
|
||||
Set<Node> set = new HashSet<>();
|
||||
queue.offer(node);
|
||||
set.add(node);
|
||||
Node cur;
|
||||
while (!queue.isEmpty()) {
|
||||
cur = queue.poll();
|
||||
System.out.println(cur.value);
|
||||
for (Node next : cur.nexts) {
|
||||
if (!set.contains(next)) {
|
||||
set.add(next);
|
||||
queue.offer(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package leo.class10_16;
|
||||
|
||||
import leo.class10_16.Graph.Node;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* @author Leo
|
||||
* @ClassName DFS
|
||||
* @DATE 2020/12/29 2:29 下午
|
||||
* @Description 深度优先遍历
|
||||
*/
|
||||
public class DFS {
|
||||
|
||||
/**
|
||||
* 功能描述 : 深度优先遍历
|
||||
* 入栈就打印
|
||||
* @author Leo
|
||||
* @date 2020/12/29 2:34 下午
|
||||
* @param node
|
||||
* @return void
|
||||
*/
|
||||
public static void dfs(Node node) {
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
Stack<Node> stack = new Stack<>();
|
||||
HashSet<Node> set = new HashSet<>();
|
||||
set.add(node);
|
||||
stack.push(node);
|
||||
Node cur;
|
||||
System.out.println(node.value);
|
||||
while (!stack.isEmpty()) {
|
||||
cur = stack.pop();
|
||||
for (Node next : cur.nexts) {
|
||||
if (!set.contains(next)) {
|
||||
set.add(next);
|
||||
/**
|
||||
* 栈永远放着整条路径
|
||||
*/
|
||||
stack.push(cur);
|
||||
stack.push(next);
|
||||
System.out.println(next.value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void dfs1(Node node) {
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
Stack<Node> stack = new Stack<>();
|
||||
Set<Node> set = new HashSet<>();
|
||||
stack.push(node);
|
||||
set.add(node);
|
||||
System.out.println(node.value);
|
||||
Node cur;
|
||||
while (!stack.isEmpty()) {
|
||||
cur = stack.pop();
|
||||
for (Node next : cur.nexts) {
|
||||
if (!set.contains(next)) {
|
||||
stack.push(next);
|
||||
set.add(next);
|
||||
System.out.println(next.value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package leo.class10_16.Graph;
|
||||
|
||||
/**
|
||||
* @author Leo
|
||||
* @ClassName Edge
|
||||
* @DATE 2020/12/29 1:57 下午
|
||||
* @Description
|
||||
*/
|
||||
public class Edge {
|
||||
/**
|
||||
* 边的权重
|
||||
*/
|
||||
public int weight;
|
||||
|
||||
/**
|
||||
* 从哪个节点出发
|
||||
*/
|
||||
public Node from;
|
||||
|
||||
/**
|
||||
* 到哪个节点
|
||||
*/
|
||||
public Node to;
|
||||
|
||||
public Edge(int weight, Node from, Node to) {
|
||||
this.weight = weight;
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package leo.class10_16.Graph;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* @author Leo
|
||||
* @ClassName Graph
|
||||
* @DATE 2020/12/29 2:00 下午
|
||||
* @Description 图:由点集合边集组成
|
||||
*
|
||||
* 1.邻接表法
|
||||
* 2.邻接矩阵法
|
||||
* 3.除此之外还有其他众多的方式
|
||||
*/
|
||||
public class Graph {
|
||||
/**
|
||||
* 点集
|
||||
* key:为点结构的值
|
||||
* value:key对应的点结构
|
||||
*/
|
||||
public HashMap<Integer,Node> nodes;
|
||||
|
||||
/**
|
||||
* 边集
|
||||
* 边集不重复
|
||||
*/
|
||||
public HashSet<Edge> edges;
|
||||
|
||||
public Graph() {
|
||||
nodes = new HashMap<>();
|
||||
edges = new HashSet<>();
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package leo.class10_16.Graph;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Leo
|
||||
* @ClassName GraphGenerator
|
||||
* @DATE 2020/12/29 2:08 下午
|
||||
* @Description 创建图
|
||||
*/
|
||||
public class GraphGenerator {
|
||||
|
||||
/**
|
||||
* 功能描述 : 数组邻接矩阵法创建图结构
|
||||
* @author Leo
|
||||
* @date 2020/12/29 2:08 下午
|
||||
* @param matrix matrix[i] = [weight,formValue,toValue]
|
||||
* @return leo.class10_16.Graph.Graph
|
||||
*/
|
||||
public static Graph createGraph(int[][] matrix) {
|
||||
Graph graph = new Graph();
|
||||
for (int i = 0; i < matrix.length; i++) {
|
||||
int weight = matrix[i][0];
|
||||
int form = matrix[i][1];
|
||||
int to = matrix[i][2];
|
||||
if (!graph.nodes.containsKey(form)) {
|
||||
graph.nodes.put(form, new Node(form));
|
||||
}
|
||||
if (!graph.nodes.containsKey(to)) {
|
||||
graph.nodes.put(form, new Node(to));
|
||||
}
|
||||
Node formNode = graph.nodes.get(form);
|
||||
Node toNode = graph.nodes.get(to);
|
||||
Edge edge = new Edge(weight, formNode, toNode);
|
||||
formNode.nexts.add(toNode);
|
||||
formNode.out++;
|
||||
toNode.in++;
|
||||
formNode.edges.add(edge);
|
||||
graph.edges.add(edge);
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
|
||||
class Train {
|
||||
public Graph createGraph(int[][] matrix) {
|
||||
Graph graph = new Graph();
|
||||
for (int i = 0; i < matrix.length; i++) {
|
||||
int weight = matrix[i][0];
|
||||
int from = matrix[i][0];
|
||||
int to = matrix[i][0];
|
||||
if (!graph.nodes.containsKey(from)) {
|
||||
graph.nodes.put(from, new Node(from));
|
||||
}
|
||||
if (!graph.nodes.containsKey(to)) {
|
||||
graph.nodes.put(to, new Node(to));
|
||||
}
|
||||
Node fromNode = graph.nodes.get(from);
|
||||
Node toNode = graph.nodes.get(to);
|
||||
Edge edge = new Edge(weight, fromNode, toNode);
|
||||
fromNode.nexts.add(toNode);
|
||||
fromNode.out++;
|
||||
toNode.in++;
|
||||
fromNode.edges.add(edge);
|
||||
graph.edges.add(edge);
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package leo.class10_16.Graph;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* @author Leo
|
||||
* @ClassName Node
|
||||
* @DATE 2020/12/29 1:53 下午
|
||||
* @Description 点结构
|
||||
*/
|
||||
public class Node {
|
||||
|
||||
/**
|
||||
* 值
|
||||
*/
|
||||
public int value;
|
||||
/**
|
||||
* 入度:指有多少边指向该节点
|
||||
*/
|
||||
public int in;
|
||||
/**
|
||||
* 出度:有多少条边指向其他点
|
||||
*/
|
||||
public int out;
|
||||
|
||||
/**
|
||||
* 邻居:从该节点出发可以到达的点为邻居
|
||||
*/
|
||||
public ArrayList<Node> nexts;
|
||||
|
||||
/**
|
||||
* 从该节点出发有哪些边
|
||||
*/
|
||||
public ArrayList<Edge> edges;
|
||||
|
||||
public Node(int value) {
|
||||
this.value = value;
|
||||
this.in = 0;
|
||||
this.out = 0;
|
||||
this.nexts = new ArrayList<>();
|
||||
this.edges = new ArrayList<>();
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package leo.class10_17;
|
||||
|
||||
/**
|
||||
* @author Leo
|
||||
* @ClassName Dijkstra
|
||||
* @DATE 2020/12/30 11:16 上午
|
||||
* @Description
|
||||
*/
|
||||
public class Dijkstra {
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package leo.class10_17;
|
||||
|
||||
/**
|
||||
* @author Leo
|
||||
* @ClassName Edge
|
||||
* @DATE 2020/12/30 10:59 上午
|
||||
* @Description 边结构
|
||||
*/
|
||||
public class Edge {
|
||||
public int weight;
|
||||
public Node from;
|
||||
public Node to;
|
||||
|
||||
public Edge(int weight, Node from, Node to) {
|
||||
this.weight = weight;
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package leo.class10_17;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* @author Leo
|
||||
* @ClassName Graph
|
||||
* @DATE 2020/12/30 11:01 上午
|
||||
* @Description 图结构
|
||||
*/
|
||||
public class Graph {
|
||||
|
||||
public HashMap<Integer, Node> nodes;
|
||||
public HashSet<Edge> edges;
|
||||
|
||||
public Graph() {
|
||||
nodes = new HashMap<>();
|
||||
edges = new HashSet<>();
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package leo.class10_17;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* @author Leo
|
||||
* @ClassName Node
|
||||
* @DATE 2020/12/30 10:58 上午
|
||||
* @Description 点结构
|
||||
*/
|
||||
public class Node {
|
||||
|
||||
public int value;
|
||||
public int in;
|
||||
public int out;
|
||||
public ArrayList<Node> nodes;
|
||||
public ArrayList<Edge> edges;
|
||||
|
||||
public Node(int value) {
|
||||
this.value = value;
|
||||
this.in = 0;
|
||||
this.out = 0;
|
||||
this.nodes = new ArrayList<>();
|
||||
this.edges = new ArrayList<>();
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package leo.class11_17;
|
||||
|
||||
import class11_17.Code01_Hanoi;
|
||||
|
||||
/**
|
||||
* @author Leo
|
||||
* @ClassName Hanoi
|
||||
* @DATE 2020/12/30 11:17 上午
|
||||
* @Description 汉诺塔问题
|
||||
*/
|
||||
public class Hanoi {
|
||||
|
||||
public static void hanoi(int n) {
|
||||
if (n == 0) {
|
||||
return;
|
||||
}
|
||||
process(n, "left", "right", "mid");
|
||||
}
|
||||
|
||||
private static void process(int n, String form, String to, String other) {
|
||||
if (n == 1) {
|
||||
System.out.println("move " + 1 + " " + form + " =>" + to);
|
||||
}else{
|
||||
process(n - 1, form, other, to);
|
||||
System.out.println("move " + n + " " + form + " =>" + to);
|
||||
process(n - 1, other, to, form);
|
||||
}
|
||||
|
||||
}
|
||||
public static void main(String[] args){
|
||||
int n = 3;
|
||||
hanoi(n);
|
||||
System.out.println("-------");
|
||||
Code01_Hanoi.hanoi2(n);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in new issue