图论算法整理

图论算法整理

图算法专题的一些基础知识。



一、图的遍历

  1. DFS
    深度优先搜索:采用递归实现,访问当前节点,然后对于所有该节点的出边。如果出边的端点没有被访问过则递归访问该节点。
#include <iostream>
#include <vector>
using namespace std;
#define MAXV 1000
#define INF 1e9
//邻接矩阵版
int n;
int G[MAXV][MAXV];
bool vis[MAXV]={
   false};
void dfs_adjmatrix(int u, int depth){
   
    vis[u]=true;
    for(int v=0;v<n;v++){
   
        if(vis[v]==false&&G[u][v]!=INF){
   
            dfs_adjmatrix(v, depth+1);
        }
    }
}
void dfs_trave(){
   
    for(int u=0;u<n;u++){
   
        if(vis[u]==false){
   
            dfs_adjmatrix(u, 1);
        }
    }
}


//邻接表版
vector<int> Adj[MAXV];
int n; //n为顶点数
bool vis[MAXV]={
   false};
void dfs_adjvec(int u, int depth){
   
    vis[u]=true;
    for(int i=0;i<Adj[u].size();i++){
   
        int v=Adj[u][i];
        if(vis[v]==false){
   
            dfs_adjvec(v, depth+1);
        }
    }
}

void dfsTrave_vec(){
   
    for(int u=0;u<n;u++){
   
        if(vis[u]==false){
   
            dfs_adjvec(u, 1);
        }
    }
}
  1. BFS
    使用队列实现广度优先遍历,先访问源点,再使用while循环,每次都将当前访问节点u的所有未访问过的邻接点加入到队列中去,直至队列为空。
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
//邻接矩阵版
#define MAXV 1000
#define INF 1e9
int n, G[MAXV][MAXV];
bool inq_1[MAXV]={
   false};
void BFS_1(int u){
   
    queue<int> q;
    q.push(u);
    inq_1[u]=true;
    while(!q.empty()){
   
        int u=q.front();
        q.pop();
        for(int v=0;v<n;v++){
   
            if(inq_1[v]==false&&G[u][v]!=INF){
   
                q.push(v);
                inq_1[v]=true;
            }
        }
    }
}
void bFSTrave_1(){
   
    for(int u=0;u<n;u++){
   
        if(inq_1[u]==false){
   
            BFS_1(u);
        }
    }
}
//邻接表版
vector<int> Adj[MAXV];
int n;
bool inq_2[MAXV]={
   false};
void BFS_2(int u){
   
    queue<int> q;
    q.push(u);
    inq_2[u]=true;
    while(!q.empty()){
   
        int u=q.front();
        q.pop();
        for(int i=0;i<Adj[u].size();i++){
   
            int v=Adj[u][i];
            if(inq_2[v]==false){
   
                q.push(v);
                inq_2[v]=true;
            }
        }
    }
}

void BFSTrave_2(){
   
    for(int u=0;u<n;u++)
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一些常用的图论算法的 MATLAB 代码: 1. 最短路径算法(Dijkstra 算法) 输入:邻接矩阵 adjacencyMatrix,起点 startNode,终点 endNode 输出:最短路径长度 shortestPathLength,最短路径 shortestPath ``` function [shortestPathLength,shortestPath] = dijkstra(adjacencyMatrix,startNode,endNode) numNodes = size(adjacencyMatrix,1); pathLengths = Inf(1,numNodes); visited = false(1,numNodes); previousNodes = zeros(1,numNodes); pathLengths(startNode) = 0; while ~all(visited) [~,index] = min(pathLengths(~visited)); currentNode = find(~visited,1,'first'); currentNode = index(1); visited(currentNode) = true; neighbors = find(adjacencyMatrix(currentNode,:)); for neighbor = neighbors tentativePathLength = pathLengths(currentNode) + adjacencyMatrix(currentNode,neighbor); if tentativePathLength < pathLengths(neighbor) pathLengths(neighbor) = tentativePathLength; previousNodes(neighbor) = currentNode; end end end shortestPathLength = pathLengths(endNode); shortestPath = endNode; while shortestPath(1) ~= startNode shortestPath = [previousNodes(shortestPath(1)), shortestPath]; end ``` 2. 最小生成树算法(Prim 算法) 输入:邻接矩阵 adjacencyMatrix 输出:最小生成树 minimumSpanningTree ``` function minimumSpanningTree = prim(adjacencyMatrix) numNodes = size(adjacencyMatrix,1); minimumSpanningTree = zeros(1,numNodes); costs = Inf(1,numNodes); visited = false(1,numNodes); minimumSpanningTree(1) = 1; visited(1) = true; for i=2:numNodes costs(i) = adjacencyMatrix(1,i); end for i=2:numNodes [~,index] = min(costs(~visited)); currentNode = find(~visited,1,'first'); currentNode = index(1); visited(currentNode) = true; minimumSpanningTree(i) = currentNode; neighbors = find(adjacencyMatrix(currentNode,:)); for neighbor = neighbors if adjacencyMatrix(currentNode,neighbor) < costs(neighbor) && ~visited(neighbor) costs(neighbor) = adjacencyMatrix(currentNode,neighbor); end end end ``` 3. 拓扑排序算法 输入:邻接矩阵 adjacencyMatrix 输出:拓扑排序结果 topologicalOrder ``` function topologicalOrder = topologicalSort(adjacencyMatrix) numNodes = size(adjacencyMatrix,1); inDegree = sum(adjacencyMatrix); queue = find(inDegree == 0); topologicalOrder = []; while ~isempty(queue) currentNode = queue(1); queue = queue(2:end); topologicalOrder = [topologicalOrder, currentNode]; neighbors = find(adjacencyMatrix(currentNode,:)); for neighbor = neighbors inDegree(neighbor) = inDegree(neighbor) - 1; if inDegree(neighbor) == 0 queue = [queue, neighbor]; end end end if length(topologicalOrder) ~= numNodes topologicalOrder = []; end ``` 以上是一些常用的图论算法的 MATLAB 代码,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值