Skip to content

Commit

Permalink
add algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
rbmonster committed Mar 5, 2023
1 parent 3d7fad0 commit 216eb32
Show file tree
Hide file tree
Showing 20 changed files with 1,076 additions and 134 deletions.
360 changes: 228 additions & 132 deletions src/main/java/com/learning/algorithm/ALGORITHM.md

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions src/main/java/com/learning/algorithm/graph/BreadthFirstPaths.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.learning.algorithm.graph;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

/**
* 广度优先搜索
*/
public class BreadthFirstPaths {

private boolean[] marked;
private int[] edgeTo; // 边

private int s; // 起点

public BreadthFirstPaths(Graph G, int s) {
marked = new boolean[G.V()];
edgeTo = new int[G.V()];
this.s = s;
bfs(G, s);
}

private void bfs(Graph G, int s) {
Deque<Integer> queue= new LinkedList<>();
queue.offer(s);
while (!queue.isEmpty()) {
int v = queue.poll();
for (int w: G.adj(v)) {
edgeTo[w] = v;
marked[w]= true;
queue.offer(w);
}
}
}


public boolean hasPathTo(int v) {
return marked[v];
}


public Iterable<Integer> pathTo(int v) {
if (!marked[v]) {
return null;
}
Stack<Integer> path = new Stack<>();
for (int x = v; x !=s; x = edgeTo[x]) {
path.push(x);
}
path.push(s);
return path;
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/learning/algorithm/graph/DFSCycle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.learning.algorithm.graph;

/**
* 深度优先搜索检测是否存在环
*/
public class DFSCycle {

private boolean[] marked;

private boolean hasCycle;

public DFSCycle(Graph G) {
this.marked = new boolean[G.V()];

}

/**
* dfs 判断是否存在环
* @param G
* @param v
* @param u
*/
private void dfs(Graph G, int v, int u) {
marked[v] = true;
for (int w: G.adj(v)) {
if (!marked[w]) {
dfs(G, w, v);
} else {
if (w != u) {
this.hasCycle = true;
}
}
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/learning/algorithm/graph/DFSTwoColor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.learning.algorithm.graph;

/**
* 深度优先搜索-二分法
*/
public class DFSTwoColor {

private boolean[] marked;

private boolean[] color;
private boolean isTwoColor = true;

public DFSTwoColor(Graph G) {
marked = new boolean[G.V()];
color = new boolean[G.V()];
for (int i = 0; i < G.V(); i++) {
if (!marked[i]) {

}
}
}

private void dfs(Graph G, int v) {
marked[v] = true;
for (Integer w : G.adj(v)) {
if (!marked[w]) {
color[w] = !color[v];
dfs(G, w);
} else if (color[w] == color[v]) {
// 节点已经访问过,但是颜色与连通节点颜色相同,说明不是二分图
this.isTwoColor = false;
}
}
}
}
55 changes: 55 additions & 0 deletions src/main/java/com/learning/algorithm/graph/DepthFirstPaths.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.learning.algorithm.graph;

import java.util.Stack;

/**
* 深度优先搜索
*/
public class DepthFirstPaths {
private boolean[] marked;
private int[] edgeTo; // 从起点到一个顶点的已知路径上的最后一个顶点
private int s; // 起点

public DepthFirstPaths(Graph G, int s) {
marked = new boolean[G.V()];
edgeTo = new int[G.V()];
this.s = s;
dfs(G, s);
}

private void dfs(Graph G, int v) {
marked[v] = true;
for (int w: G.adj(v)) {
if (!marked[w]) {
edgeTo[w] = v;
dfs(G, w);
}
}
}

/**
* 是否与起点s 连通
* @param v
* @return
*/
public boolean hasPathTo(int v) {
return marked[v];
}

/**
* 返回路径
* @param v
* @return
*/
public Iterable<Integer> pathTo(int v) {
if (!marked[v]) {
return null;
}
Stack<Integer> path = new Stack<>();
for (int x = v; x !=s; x = edgeTo[x]) {
path.push(x);
}
path.push(s);
return path;
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/learning/algorithm/graph/Digraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.learning.algorithm.graph;

import java.util.HashSet;
import java.util.Set;

/**
* 有向图
*/
public class Digraph {
private final int V; // 顶点数目
private int E; // 边的数目

private Set<Integer>[] adj; // 邻接表

public Digraph(int V) {
this.V = V;
this.E = 0;
adj = new HashSet[V];
for (int i = 0; i < V; i++) {
adj[i] = new HashSet<>();
}
}

/**
* 有向图添加边
* @param v
* @param w
*/
public void addEdge(int v,int w) {
adj[v].add(w);
E++;
}

public Iterable<Integer> adj(int v) {
return adj[v];
}

public int V(){
return V;
}

public int E() {
return E;
}


}
54 changes: 54 additions & 0 deletions src/main/java/com/learning/algorithm/graph/DirectEdge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.learning.algorithm.graph;

import java.util.Objects;

/**
* 加权有向边
*/
public class DirectEdge implements Comparable<DirectEdge>{
private final int v;
private final int w;
private final double weight; // 权重

public DirectEdge(int v, int w, double weight) {
this.v = v;
this.w = w;
this.weight = weight;
}

public double weight() {
return weight;
}

public int from() {
return v;
}

public int to() {
return w;
}

@Override
public int compareTo(DirectEdge that) {
if (this.weight() <that.weight()) {
return -1;
} else if (this.weight() > that.weight()) {
return 1;
} else {
return 0;
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DirectEdge edge = (DirectEdge) o;
return v == edge.v && w == edge.w && Double.compare(edge.weight, weight) == 0;
}

@Override
public int hashCode() {
return Objects.hash(v, w, weight);
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/learning/algorithm/graph/DirectedDFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.learning.algorithm.graph;

/**
* 有向图 DFS
*/
public class DirectedDFS {

private boolean[]marked;

public DirectedDFS(Digraph G, int s) {
marked = new boolean[G.V()];

}

private void dfs(Digraph G, int v) {
marked[v] = true;
for (int w: G.adj(v)) {
if (!marked[w]) {
dfs(G, w);
}
}
}


}
70 changes: 70 additions & 0 deletions src/main/java/com/learning/algorithm/graph/Edge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.learning.algorithm.graph;

import java.util.Objects;

/**
* 加权无向边
*/
public class Edge implements Comparable<Edge>{

private final int v;
private final int w;
private final double weight; // 权重

public Edge(int v, int w, double weight) {
this.v = v;
this.w = w;
this.weight = weight;
}

public double weight() {
return weight;
}

/**
* 两边其中一个端点之一
* @return
*/
public int either() {
return v;
}

/**
* 获取边 顶点vertex 连通的另一个顶点
* @param vertex
* @return
*/
public int other(int vertex) {
if (vertex == v) {
return w;
} else if (vertex == w) {
return v;
} else {
throw new RuntimeException("Inconsistent edge");
}
}

@Override
public int compareTo(Edge that) {
if (this.weight() <that.weight()) {
return -1;
} else if (this.weight() > that.weight()) {
return 1;
} else {
return 0;
}
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Edge edge = (Edge) o;
return v == edge.v && w == edge.w && Double.compare(edge.weight, weight) == 0;
}

@Override
public int hashCode() {
return Objects.hash(v, w, weight);
}
}
Loading

0 comments on commit 216eb32

Please sign in to comment.