思考
- 微信好友关系
- QQ推荐有可能认识的人
- 图数据库 Neo4j
- 知识图谱 推荐算法、数据挖掘
- 导航软件路径推荐,迪杰斯特拉算法
图–解决关系型网络系统
图的基本知识:
- 顶点:
- 边:
- 顶点的度:
- 出度、入度
- 有向图
- 无向图
存储: 数组+链表
解决关系型网络系统
邻接矩阵
图里有x个点就是 x*x的矩阵
7*7
的矩阵,有0和1
A[7][7]
: 巧妙的应用数组的下标A[1][1]
表示从1到1的情况A[1][2]
就表示1到2的情况,有边标识A[1][2]=1
, 没边标识A[1][3]=0
稀疏矩阵:3*3的矩阵
0 0 0
1 1 1
0 1 0
邻接表:链表
-
数组:浪费空间,但是速度块。数据不大 优先选用数组
-
链表:节省空间,但是速度慢
图的遍历 搜索算法
案例:
解救美女:有一天,小美和你去玩迷宫。但是方向感不好的小美很快就迷路了,你得知后便去
解救无助的小美,你已经弄清楚了迷宫的地图,现在你要知道从你当前位置出发你是否能够达到
小美的位置?
1:表示地图上的障碍物,0表示有路可走
邻接矩阵:
0(您) | 0 | 1 | 0 |
0 | 0 | 0 | 0 |
0 | 0 | 1 | 0 |
0 | 1 | 0(小美) | 0 |
0 | 0 | 0 | 1 |
深度优先遍历(DFS)
大家可以想象玩迷宫,是不是选择一个方向走到底,直到不能走了你在返回一步继续试其他的方向,没错这其实就是深度优先遍历。一条路走到底,递归,有回溯。也要标记走过的点
关键的优化: 剪枝
面试中很容易面到搜索算法。机试
直接面试问 最容易问树
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
//深度遍历 最短路线
class DFS {
int line; //地图行
int column; //地图列
int dx; //目标x
int dy; //目标y
int data[][]; //邻街矩阵
boolean mark[][]; //标记数据
int minStep = Integer.MAX_VALUE;
int next[][] = {
{
0,1}, {
1,0}, {
0,-1}, {
-1,0}}; // ACM 想到的
Stack<Point> stack = new Stack<>(); //保存当前路径
//保存所有的路径,key是步数
Map<Integer, List<Stack<Point>>> result = new HashMap<>();
public DFS(int line, int column, int dx, int dy, int[][] data, boolean[][] mark) {
this.line = line;
this.column = column;
this.dx = dx;
this.dy = dy;
this.data = data;
this.mark = mark;
}
public void dfs(int sx, int sy, int step) {
if (sx == dx && sy == dy) {
if (!result.containsKey(step)) {
result.put(step, new ArrayList<>());
}
result.get(step).add((Stack<Point>) stack.clone());
if (step < minStep ) minStep = step;
return;
}
for (int i = 0; i < 4; i++) {
int nextX = sx + next[i][0];
int nextY = sy + next[i][1];
if (nextX < 0 || nextX > line || nextY < 0 || nextY > column) continue;
if (data[nextX][nextY] == 0 && !mark[nextX][nextY]) {
mark[nextX][nextY] = true;
stack.add(new Point(nextX, nextY));
dfs(nextX, nextY, step+1);
mark[nextX][nextY] = false; //回溯
stack.pop();
}
}
}
public static void main(String[] args) {
int data[][] = {
{
0,0,1,0},
{
0,0,0,0},
{
0,0,1,0},
{
0,1,0,0},
{
0,0,0,1}
};
int dx = 3;
int dy = 2;
int sx = 0;
int sy = 0;
int line = 4;
int column = 3;
boolean mark[][] = new boolean[line+1][column+1];
DFS dfs = new DFS(line, column, dx, dy, data, mark);
dfs.dfs(sx, sy, 0);
System.out.println(dfs.minStep);
List<Stack<Point>> stacks = dfs.result.get(dfs.minStep);
for (Stack<Point> stack : stacks) {
for (Point point : stack) {
System.out.printf("(" + point.x + ", " + point.y + ")");;
}
System.out.println();
}
}
}
广度优先遍历(BFS) ---- ArrayBlockingQueue
类似于树结构的层次遍历,先找到一个点,然后把该点加入队列,依次找出该点的关联边加入队列,循环操作,一直到队列为空,开始就把所有的路都给走了
两个关键点:队列,标记数组,加过的点不能再加
启发式搜索,A*
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
//广度遍历 能不能到达
class BFS {
int line; //地图行
int column; //地图列
int dx; //目标x
int dy; //目标y
int data[][]; //邻街矩阵
boolean mark[][]; //标记数据
int next[][] = {
{
0,1}, {
1,0}, {
0,-1}, {
-1,0}}; // ACM 想到的
public BFS(int line, int column, int dx, int dy, int[][] data, boolean[][] mark) {
this.line = line;
this.column = column;
this.dx = dx;
this.dy = dy;
this.data = data;
this.mark = mark;
}
public boolean bfs(int sx, int sy) {
//当前位置 x,y当前位置 求(x,y)->(dx,dy)
if (sx < 0 || sx > line || sy < 0 || sy > column) return false;
if (sx == dx && sy == dy) {
System.out.println("当前位置就是目标位置: sx = " + sx + ", sy = " + sy);
return true;
}
mark[sx][sy] = true;
Queue<Point> queue = new ArrayBlockingQueue<>(line * column);
queue.add(new Point(sx,sy));
while (!queue.isEmpty()) {
Point point = queue.poll(); //队列第一个点
for (int i = 0; i < 4; i++) {
int nextX = point.x + next[i][0];
int nextY = point.y + next[i][1];
if (nextX < 0 || nextX > line || nextY < 0 || nextY > column) continue;
if (data[nextX][nextY] == 0 && !mark[nextX][nextY]) {
if (nextX == dx && nextY == dy) {
System.out.println("找到了: dx = " + dx + ", dy = " + dy);
return true;
}
mark[nextX][nextY] = true;
queue.add(new Point(nextX, nextY));
}
}
}
return false;
}
public static void main(String[] args) {
int data[][] = {
{
0,0,1,0},
{
0,0,0,0},
{
0,0,1,0},
{
0,1,0,0},
{
0,0,0,1}
};
int dx = 3;
int dy = 2;
int sx = 0;
int sy = 0;
int line = 4;
int column = 3;
boolean mark[][] = new boolean[line+1][column+1];
BFS bfs = new BFS(line, column, dx, dy, data, mark);
bfs.bfs(sx,sy);
}
}
最短路径–迪杰斯特拉算法
最短路径核心思想分析:贪心:排序,贪心策略。1-3 我们认为是10,局部
- 我们开一个dis数组,用来表示起始点到每个顶点的距离,最开始时我们赋值为无穷大
- 加变量loc,初始赋值为起始点
- 通过loc更新dis数组,因为加入一个点后我们就可以更新路径
贪心策略:在dis数组里面找离初始点最近的那个点
- 在dis数组里面找离初始点最近的那个点,排除已经选择过的点,将之赋值给loc
- 重复执行 3 4操作,直到所有点加完
//地图--最短路径 迪杰斯特拉算法(Dijkstra)
/**
6 个点
8 个变
1 是起点
1 3 10
1 5 30
1 6 100
2 3 5
3 4 50
4 6 10
5 4 20
5 6 60
1 到 1 的最短距离是 0 === 最短路径是 1
1 到 2 的最短距离是 2147483647 === 无法到达
1 到 3 的最短距离是 10 === 最短路径是 1 -> 3
1 到 4 的最短距离是 50 === 最短路径是 1 -> 5 -> 4
1 到 5 的最短距离是 30 === 最短路径是 1 -> 5
1 到 6 的最短距离是 60 === 最短路径是 1 -> 5 -> 4 -> 6
*/
class DJSTL {
static Map<Integer, String> routes = new HashMap<>();
public static void search(int start, int dis[], int value[][], int point) {
boolean mark[] = new boolean[point+1];
mark[start] = true;
dis[start] = 0;
int count = 1;
while (count <= point) {
//O(n^2)
//求最小值点开始
int loc = 0; //新加的点
int min = Integer.MAX_VALUE;
for (int i = 1; i <= point; i++) {
//求dis里面最小的值 可以优化成 堆 logn
if (!mark[i] && dis[i] < min) {
min = dis[i];
loc = i;
}
}
if (loc == 0) break; //表示没有可以加入的点
mark[loc] = true;
if (routes.get(loc) == null) {
routes.put(loc, start + " -> " + loc);
}
//加入的点到各点距离计算
//优化只需要关注加入的点
for (int i = 1; i <= point; i++) {
//min(dis[3] + data[3][4], dis[4])
if ( value[loc][i] != -1 && (dis[loc] + value[loc][i] < dis[i]) ) {
dis[i] = dis[loc] + value[loc][i];
routes.put(i, routes.get(loc) + " -> " + i);
}
}
count++;
}
for (int i = 1; i <= point; i++) {
System.out.print(start + " 到 " + i + " 的最短距离是 " + dis[i] + " === ");
if (dis[i] == 0 && routes.get(i) == null) {
System.out.println("最短路径是 " + i);
} else if (dis[i] == Integer.MAX_VALUE) {
System.out.println("无法到达");
} else {
System.out.println( "最短路径是 " + routes.get(i));
}
}
}
public static void main(String[] args) {
int point, line, start; //n 点数, m 边数, x 起点
Scanner scanner = new Scanner(System.in);
point = scanner.nextInt();
line = scanner.nextInt();
start = scanner.nextInt();
int value[][] = new int[point+1][line+1]; //点到点矩阵
int dis[] = new int[point+1]; //存储最短路径
for (int i = 1; i <= point; i++) {
dis[i] = Integer.MAX_VALUE;
for (int j = 1; j <= point; j++) {
//初始化地图
if (i == j) {
value[i][j] = 0;
} else {
value[i][j] = -1;
}
}
}
for (int i = 0; i < line; i++) {
int xx = scanner.nextInt();
int yy = scanner.nextInt();
int v = scanner.nextInt(); //xx 到 yy 的距离
value[xx][yy] = v;
if (xx == start) {
dis[yy] = v;
}
}
search(start, dis, value, point);
}
}