pull/3/head
Leo 5 years ago
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,15 @@
package leo.class10_16;
/**
* @author Leo
* @ClassName Dijkstra
* @DATE 2020/12/30 10:05
* @Description Dijkstra
* ,,<>
* 1Dijkstra
* 20
* 3
* 4
*/
public class Dijkstra {
}

@ -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,95 @@
package leo.class10_16;
import leo.class10_16.Graph.Edge;
import leo.class10_16.Graph.Graph;
import leo.class10_16.Graph.Node;
import java.util.*;
/**
* @author Leo
* @ClassName Kruskal
* @DATE 2020/12/29 5:46
* @Description Kruskal
* 1
* 2
* 3
* 4
* 5
*/
public class Kruskal {
class Code{
class UnionFind{
Map<Node, Node> parentMap;
Map<Node, Integer> sizeMap;
public UnionFind(Collection<Node> nodes) {
parentMap = new HashMap<>();
sizeMap = new HashMap<>();
Iterator<Node> iterator = nodes.iterator();
while (iterator.hasNext()) {
Node cur = iterator.next();
parentMap.put(cur, cur);
sizeMap.put(cur, 0);
}
}
public Node find(Node node) {
Stack<Node> stack = new Stack<>();
while (node != parentMap.get(node)) {
stack.push(node);
node = parentMap.get(node);
}
while (!stack.isEmpty()) {
parentMap.put(stack.pop(), node);
}
return node;
}
public boolean isSameSet(Node n1, Node n2) {
return find(n1) == find(n2);
}
public void union(Node n1, Node n2) {
Node aF = find(n1);
Node bF = find(n2);
if (n1 == n2) {
return;
}
int aSize = sizeMap.get(aF);
int bSize = sizeMap.get(bF);
Node big = aSize >= bSize ? aF : bF;
Node small = big == aF ? bF : aF;
parentMap.put(small, big);
sizeMap.put(big, aSize + bSize);
sizeMap.remove(small);
}
}
public Set<Edge> kruskalMST(Graph graph){
UnionFind unionFind = new UnionFind(graph.nodes.values());
PriorityQueue<Edge> queue = new PriorityQueue<>((o1,o2)->{return o1.weight - o2.weight;});
for (Edge edge : graph.edges) {
queue.offer(edge);
}
Set<Edge> set = new HashSet<>();
while (!queue.isEmpty()) {
Edge edge = queue.poll();
if (!unionFind.isSameSet(edge.from, edge.to)) {
set.add(edge);
unionFind.union(edge.from, edge.to);
}
}
return set;
}
}
}

@ -0,0 +1,64 @@
package leo.class10_16;
import com.sun.org.apache.regexp.internal.RE;
import leo.class10_16.Graph.Edge;
import leo.class10_16.Graph.Graph;
import leo.class10_16.Graph.Node;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;
/**
* @author Leo
* @ClassName Prim
* @DATE 2020/12/30 10:04
* @Description Prim
* 1
* 2
* 3
* 43
* 52
* 6
*
*/
public class Prim {
public static Set<Edge> primMST(Graph graph) {
//根据权重排序 小根堆
PriorityQueue<Edge> queue = new PriorityQueue<>((o1, o2) -> {return o1.weight - o2.weight;});
for (Edge edge : graph.edges) {
queue.offer(edge);
}
Set<Node> set = new HashSet<>();
Set<Edge> result = new HashSet<>();
for (Node node : graph.nodes.values()) {
//点解锁边
if (!set.contains(node)) {
for (Edge edge : node.edges) {
queue.offer(edge);
}
}
//边解锁点
while (!queue.isEmpty()) {
Edge edge = queue.poll();
Node toNode = edge.to;
//点又解锁边
if (!set.contains(toNode)) {
set.add(toNode);
result.add(edge);
for (Edge e : toNode.edges) {
queue.offer(e);
}
}
}
// 如果要防森林,就加上break,如果确定是一个图,就取消break
//break
}
return result;
}
}

@ -0,0 +1,245 @@
package leo.class10_16;
import leo.class10_16.Graph.Graph;
import leo.class10_16.Graph.Node;
import java.lang.reflect.Array;
import java.util.*;
/**
* @author Leo
* @ClassName TopologicalOrder
* @DATE 2020/12/29 3:13
* @Description
*
*
* :
* xSum>ySum
* xSumx
* ySumy
* xSum>ySum xy
* https://www.lintcode.com/problem/topological-sorting
*/
public class TopologicalOrder {
/*
10
200
3
*/
static class Sort{
public static List<Node> sortedTopology(Graph graph) {
//入度map
HashMap<Node, Integer> inMap = new HashMap<>();
//入度入度为零的队列
Queue<Node> zeroQueue = new LinkedList<>();
//变量所有node得到in为0的节点,加入到队列中
for (Node node : graph.nodes.values()) {
inMap.put(node, node.in);
if (node.in == 0) {
zeroQueue.offer(node);
}
}
List<Node> list = new ArrayList<>();
while (!zeroQueue.isEmpty()) {
Node cur = zeroQueue.poll();
//将入度为零的节点加入到结果中
list.add(cur);
for (Node next : cur.nexts) {
//将当前节点的邻居节点的入度减一
inMap.put(next, inMap.get(next) - 1);
//再次将入度为零的节点加入到队列中
if (next.in == 0) {
zeroQueue.offer(next);
}
}
}
return list;
}
}
/**
* :
* xSum>ySum
* xSumx
* ySumy
* xSum>ySum xy
*/
static class DFS_Count {
static class Record{
//节点
DirectedGraphNode node;
//节点的点次
long count;
public Record(DirectedGraphNode node, long deep) {
this.node = node;
this.count = deep;
}
}
/*
* @param graph: A list of Directed graph node
* @return: Any topological order for the given graph.
*/
public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
//点次缓存表,结果表
HashMap<DirectedGraphNode, Record> order = new HashMap<>();
for (DirectedGraphNode node : graph) {
findRecord(node, order);
}
ArrayList<Record> recordList = new ArrayList<>();
for (Record record : order.values()) {
recordList.add(record);
}
recordList.sort(new MyComparator());
ArrayList<DirectedGraphNode> list = new ArrayList<>();
for (Record record : recordList) {
list.add(record.node);
}
return list;
}
/**
* : cur
* @author Leo
* @date 2020/12/29 4:47
* @param cur
* @param order
* @return Record (,)
*/
public static Record findRecord(DirectedGraphNode cur, HashMap<DirectedGraphNode, Record> order) {
//查询缓存中是否存在,如果存在直接返回
if (order.containsKey(cur)) {
return order.get(cur);
}
//如果没有 开始计算
long count = 0;
for (DirectedGraphNode next : cur.neighbors) {
count += findRecord(next, order).count;
}
//最后要加上自己的点次
Record record = new Record(cur, count+1);
//加入缓存
order.put(cur, record);
return record;
}
//倒序
static class MyComparator implements Comparator<Record> {
@Override
public int compare(Record o1, Record o2) {
return o1.count == o2.count ? 0 :(o1.count > o2.count ? -1 : 1);
}
}
}
/**
*
* xDeep >= yDeep
* xDeep x
* yDeep y
* xDeep >= yDeep xy
*/
class DFS_Deep {
class Record{
DirectedGraphNode node;
int deep;
public Record(DirectedGraphNode node, int deep) {
this.node = node;
this.deep = deep;
}
}
public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
HashMap<DirectedGraphNode, Record> order = new HashMap<>();
for (DirectedGraphNode node : graph) {
findRecord(node, order);
}
ArrayList<Record> records = new ArrayList<>();
for (Record record : order.values()) {
records.add(record);
}
records.sort((o1,o2)->{return o2.deep - o1.deep;});
ArrayList<DirectedGraphNode> list = new ArrayList<>();
for (Record record : records) {
list.add(record.node);
}
return list;
}
public Record findRecord(DirectedGraphNode cur, HashMap<DirectedGraphNode, Record> order) {
if (order.containsKey(cur)) {
return order.get(cur);
}
int deep = 0;
for (DirectedGraphNode next : cur.neighbors) {
deep = Math.max(deep, findRecord(next, order).deep);
}
Record record = new Record(cur, deep + 1);
order.put(cur, record);
return record;
}
}
class BFS{
public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
Map<DirectedGraphNode, Integer> map = new HashMap<>();
Queue<DirectedGraphNode> zeroQueue = new LinkedList<>();
for (DirectedGraphNode node : graph) {
map.put(node, 0);
}
for (DirectedGraphNode node : graph) {
for (DirectedGraphNode next : node.neighbors) {
map.put(next, map.get(next) + 1);
}
}
for (DirectedGraphNode node : map.keySet()) {
if (map.get(node) == 0) {
zeroQueue.offer(node);
}
}
ArrayList<DirectedGraphNode> list = new ArrayList<>();
DirectedGraphNode cur;
while (!zeroQueue.isEmpty()) {
cur = zeroQueue.poll();
list.add(cur);
for (DirectedGraphNode next : cur.neighbors) {
map.put(next, map.get(next) - 1);
if (map.get(next) == 0) {
zeroQueue.offer(next);
}
}
}
return list;
}
}
/**
* @author Leo
* @date 2020/12/29 4:39
*
*/
public static class DirectedGraphNode {
public int label;
public ArrayList<DirectedGraphNode> neighbors;
public DirectedGraphNode(int x) {
label = x;
neighbors = new ArrayList<DirectedGraphNode>();
}
}
}

@ -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);
}
}

@ -0,0 +1,131 @@
package leo.class11_17;
import java.nio.file.Path;
import java.util.*;
/**
* @author Leo
* @ClassName PrintAllSubsquences
* @DATE 2020/12/30 11:16
* @Description
*/
public class PrintStr {
/**
*
*/
static class Subs{
public static List<String> subs(String string) {
List<String> list = new ArrayList<>();
if (string == null) {
return list;
}
char[] chars = string.toCharArray();
String path = "";
p(chars, 0, list, path);
return list;
}
private static void p(char[] chars, int i, List<String> list, String path) {
if (i == chars.length) {
list.add(path);
return;
};
p(chars, i + 1, list, path);
p(chars, i + 1, list, path + String.valueOf(chars[i]));
}
}
/**
*
*/
static class SubNoRepeat {
public static List<String> subNoRepeat(String string) {
List<String> list = new ArrayList<>();
if (string == null) {
return list;
}
HashSet<String> set = new HashSet<>();
char[] chars = string.toCharArray();
String path = "";
p(chars, 0, set, path);
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
list.add(iterator.next());
}
return list;
}
private static void p(char[] chars, int i, HashSet<String> set, String path) {
if (i == chars.length) {
set.add(path);
return;
} else {
p(chars, i + 1, set, path);
p(chars, i + 1, set, path + String.valueOf(chars[i]));
}
}
}
/**
*
*/
static class Permutations{
}
/**
*
*/
static class PermutationsNoRepeat{
public static String[] permutation(String s) {
if (s == null || s.length() == 0) {
return new String[]{};
}
char[] chars = s.toCharArray();
List<String> list = new ArrayList<>();
f(chars,0,list);
return list.toArray(new String[list.size()]);
}
public static void f(char[] chars,int i,List<String> list) {
if (i == chars.length) {
list.add(String.valueOf(chars));
}else {
boolean[] verify = new boolean[26];
for (int j = i; j < chars.length; j++) {
if (!verify[chars[j] - 'a']) {
verify[chars[j] - 'a'] = true;
swap(chars, i, j);
f(chars, i + 1, list);
swap(chars, i, j);
}
}
}
}
private static void swap(char[] chars,int i,int j) {
if (chars[i] == chars[j] || i == j) {
return;
}
char tmp = chars[i];
chars[i] = chars[j];
chars[j] = tmp;
}
}
public static void main(String[] args){
String str = "abcc";
List<String> subs = Subs.subs(str);
System.out.println(subs.toString());
List<String> subNoRepeat = SubNoRepeat.subNoRepeat(str);
System.out.println(subNoRepeat.toString());
String[] permutation = PermutationsNoRepeat.permutation(str);
System.out.println(new ArrayList<String>(Arrays.asList(permutation)).toString());
}
}

@ -0,0 +1,50 @@
package leo.class11_17;
import java.util.Stack;
/**
* @author Leo
* @ClassName ReverseStackUsingRecursive
* @DATE 2020/12/30 2:46
* @Description
*
*
* 使
*/
public class ReverseStackUsingRecursive {
static class Code{
public static void reverse(Stack<Integer> stack) {
if (stack.isEmpty()) {
return;
}
int i = f(stack);
reverse(stack);
stack.push(i);
}
private static int f(Stack<Integer> stack) {
int value = stack.pop();
if (stack.isEmpty()){
return value;
}
int last = f(stack);
stack.push(value);
return last;
}
}
public static void main(String[] args){
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
Code.reverse(stack);
System.out.println(stack);
}
}
Loading…
Cancel
Save