[CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径

4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes.

LeetCode和CareerCup中关于图的题都不是很多,LeetCode中只有三道,分别是Clone Graph 无向图的复制Course Schedule 课程清单Course Schedule II 课程清单之二。目前看来CareerCup中有关图的题在第四章中仅此一道,这是一道关于有向图的题,书中是用java来做的,我们用c++来做时要定义图和节点,这里参考了之前那道Clone Graph 无向图的复制中关于无向图的定义,并且在里面加入了一个枚举类变量state,来帮助我们遍历。这种找两点之间路径的题就是遍历的问题,可以用BFS或DFS来解,先来看BFS的解法,如下所示:

//Definition for directed graph.
enum State {
    Unvisited, Visited, Visiting
};

struct DirectedGraphNode {
   int label;
   State state;
   vector<DirectedGraphNode *> neighbors;
   DirectedGraphNode(int x) : label(x) {};
};

struct DirectedGraph {
    vector<DirectedGraphNode*> nodes;
};

class Solution {
public:
    bool search(DirectedGraph *g, DirectedGraphNode *start, DirectedGraphNode *end) {
        queue<DirectedGraphNode*> q;
        for (auto a : g->nodes) a->state = Unvisited;
        start->state = Visiting;
        q.push(start);
        while (!q.empty()) {
            DirectedGraphNode *node = q.front(); q.pop();
            for (auto a : node->neighbors) {
                if (a->state == Unvisited) {
                    if (a == end) return true;
                    else {
                        a->state = Visiting;
                        q.push(a);
                    }
                }
            }
            node->state = Visited;
        }
        return false;
    }
};
时间: 2024-10-19 07:01:49

[CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径的相关文章

Route Between Two Nodes in Graph (java)

Given a directed graph, design an algorithm to find out whether there is a route between two nodes. Example Given graph: A----->B----->C \ | \ | \ | \ v ->D----->E for s = B and t = E, return true for s = D and t = C, return false /** * Defini

Route Between Two Nodes in Graph

Given a directed graph, design an algorithm to find out whether there is a route between two nodes. Have you met this question in a real interview? Yes Example Given graph: A----->B----->C \ | \ | \ | \ v ->D----->E for s = B and t = E, return

Lintcode: Route Between Two Nodes in Graph

Given a directed graph, design an algorithm to find out whether there is a route between two nodes. Have you met this question in a real interview? Yes Example Given graph: A----->B----->C \ | \ | \ | \ v ->D----->E for s = B and t = E, return

LA 3353 Optimal Bus Route Design 二分匹配和有向图中的环

题意:题目给出一个有向图 , 找若干个圈,使得每个结点切好属于一个圈,并且所有圈的总长度最小 , 如果没有满足条件的就输出 'N' . 注意:1.有重边 2.如果有向边(u , v) , (v , u)都存在 , 它们的长度不一定相同. 解法: 刚看这个题目的时候,没有什么思路,知道是用二分匹配之后就更没思路了.这题的关键还是在于构图: 每个点分成入度点和出度点两个点,然后在从新建图,例如:u 分成 u1 , u2 , v 分成 v1 , v2 , 假如有 (u , v) 这条边 , 那么就变成

[LeetCode] 882. Reachable Nodes In Subdivided Graph 细分图中的可到达结点

Starting with an?undirected?graph (the "original graph") with nodes from?0?to?N-1, subdivisions are made to some of the edges. The graph is given as follows:?edges[k]?is a list of integer pairs?(i, j, n)?such that?(i, j)?is an edge of the origin

关于app.js和route.js和service.js还有controller.js中的依赖关系

2.只要是由路由去执行的的控制器模块,必须注入到app.js里面进行依赖,在页面上就不需要ng-controller在html页面上写了: 但是如果一个控制器模块,没有经过路由管理:那么就必须要,在页面上使用ng-controller='模块名',来告诉控制器可以管理这部分内容

882. Reachable Nodes In Subdivided Graph

题目链接 https://leetcode.com/contest/weekly-contest-96/problems/reachable-nodes-in-subdivided-graph/ 解题思路 1)题目要求,经过m步后,可以到达的点,等价于求有多少点距离起点的最短距离小于等于m,即这是一个单源最短路径问题,使用djstra算法 复杂度 时间 o(eloge) 空间复杂度o(e). e为边数 本解决方案的注意点 1)计数时,节点和边上的点要分开计数,防止重复计算节点 2)使用优先队列保

CareerCup All in One 题目汇总 (未完待续...)

Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation String 1.4 Replace Spaces 1.5 Compress String 1.6 Rotate Image 1.7 Set Matrix Zeroes 1.8 String Rotation Chapter 2. Linked Lists 2.1 Remove Duplicates

CareerCup All in One 题目汇总

Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation String 1.4 Replace Spaces 1.5 Compress String 1.6 Rotate Image 1.7 Set Matrix Zeroes 1.8 String Rotation Chapter 2. Linked Lists 2.1 Remove Duplicates