Find if there is a path between two vertices in an undirected graph

如图:

python bfs 预热 (简单粗暴)

# 无向图的的两个节点是否相连 ~ :
from collections import deque
def addEdge(v, w):
    global adj
    adj[v].append(w)
    adj[w].append(v)


def isReachable(s, d, V):
    if (s == d):
        return True
    visited = [False for i in range(V)]
    queue = deque()

    visited[s] = True
    queue.append(s)

    while (len(queue) > 0):
        s = queue.popleft()
        for i in adj[s]:
            if i == d:
                return True
            if not visited[i]:
                visited[i] = True
                queue.append(i)
    return False


if __name__ == "__main__":
    V = 4
    adj = [[] for i in range(V+1)]
    addEdge(0, 1)
    addEdge(0, 2)
    addEdge(1, 2)
    addEdge(2, 0)
    addEdge(2, 3)
    addEdge(3, 3)
    u, v = 1, 3
    if isReachable(u, v, V):
        print("there is a path!")
    else:
        print("there is no path!")

c++ bfs 预热


 path between two vertices in undirected graph

#include <queue>
#include <iostream>
using namespace std;

vector<vector<int>> adj;

void addEdge(int v, int w) {
    adj[v].push_back(w);
    adj[w].push_back(v);
}

bool isReachable(int s, int d) {
    if (s == d)
        return true;

    int n = (int)adj.size();
    vector<bool> visited(n, false);

    queue<int> q;

    visited[s] = true;
    q.push(s);

    while (!q.empty()) {
        s = q.front();
        q.pop();

        for (auto x:adj[s]) {
            if (x == d)
                return true;
            if (!visited[x]){
                visited[x] = true;
                q.push(x);
            }
        }
    }
    return false;
}


int main()
{
    int n = 4;
    // Create a graph in the above diagram
    adj = vector<vector<int>>(n);

    addEdge(0,1);
    addEdge(0,2);
    addEdge(1,2);
    addEdge(2,0);
    addEdge(2,3);
    addEdge(3,3);

    int u = 1, v = 3;
    if (isReachable(u, v))
        cout << "\n There is a path from " << u << " to " << v;
    else
        cout << "\n There is no path from " << u << " to " << v;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Title An Introduction to Combinatorics and Graph Theory Authors David Guichard Publisher: David Guichard (February 18, 2017) Language: English Book Description Combinatorics is a branch of mathematics concerning the study of finite or countable discrete structures. Aspects of combinatorics include counting the structures of a given kind and size (enumerative combinatorics), deciding when certain criteria can be met, and constructing and analyzing objects meeting the criteria (as in combinatorial designs and matroid theory), finding "largest", "smallest", or "optimal" objects (extremal combinatorics and combinatorial optimization), and studying combinatorial structures arising in an algebraic context, or applying algebraic techniques to combinatorial problems (algebraic combinatorics). Graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects. A "graph" in this context is made up of "vertices" or "nodes" and lines called edges that connect them. A graph may be undirected, meaning that there is no distinction between the two vertices associated with each edge, or its edges may be directed from one vertex to another; see graph (mathematics) for more detailed definitions and for other variations in the types of graph that are commonly considered. Graphs are one of the prime objects of study in discrete mathematics. This book walks the reader through the classic parts of Combinatorics and graph theory, while also discussing some recent progress in the area: on the one hand, providing material that will help students learn the basic techniques, and on the other hand, showing that some questions at the forefront of research are comprehensible and accessible to the talented and hardworking undergraduate.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值