Directed Graph Loop detection and if not have, path to print all path.

这里总结针对一个并不一定所有点都连通的general directed graph, 去判断graph里面是否有loop存在, 收到启发是因为做了[LeetCode] 207 Course Schedule_Medium tag: BFS, DFS, 这个题实际上就是监测directed graph里面是否有loop存在. 我在网上看了比较经典的做法为DFS, 并且用三个set去标志not visited点,(0), 正在visiting的点(-1), 已经visited过的点(1), 我结合这个思路, 用dictionary去"模拟"这个的三个set, 分别用0, -1, 1 表示not visited, visiting, and visited. 

 

1) Check whether have loop in directed graph

实际上就是找backedge, 如果判断edge是backedge呢, 就是说从node出发的指针指向了node自己或者它的祖先node, 那么表明是backedge, 同时也表明了有loop存在. 所以实际上就是用DFS去依次访问每个node, 如果node是1, 表明visited过了(并且从node出发的所有path都被visited过了), 就continue, 如果是-1, 表明visiting但是再次被visited, 所以直接返回False, 否则没有visited过, 将其标志为-1, visiting, 然后直到把所有从node出发的path的node都监测一遍没问题, 再将node tag为1, 表明visited过了, 并且返回continue. 直到所有的点都没返回False, 那么返回True.

参考视频虽然是老印口音,但是有字幕, 讲的还是很清楚的.

code 如下:

 1 class Solution:
 2     def checkLoopInDirectedGraph(self, graph, n): # n = number of nodes
 3         d = collections.Counter() #default value is 0, not visited
 4         def dfs(d, graph, i):
 5             if d[i] == -1: return False
 6             if d[i] == 1: return True
 7             d[i] = -1
 8             for neig in graph[i]:
 9                 if not dfs(d, graph, neig):
10                     return False
11             d[i] = 1
12             return True
13         for i in range(n):
14             if not dfs(d, graph, i):
15                 return False
16         return True

 

2) Check whether have loop in directed graph, if not loop, print path from start node to end, order does not matter for not connected, else return []

所以这个path的返回, 因为对不是联通的graph part的order无所谓, 所以我们只需要将以上的code加一行即可, 就是在visited node return True之前, 将其append进入到ans里面, 这样的话ans的顺序就是从尾巴print到node head, 所以返回的时候将ans reverse即可.

这个思路可以运用在[LeetCode] 210. Course Schedule II.

code如下:

 1 class Solution:
 2     def pathInDirectedGraph:(self, graph, n):
 3         d, ans = collections.Counter(), []
 4         def dfs(d, graph, i):
 5             if d[i] == 1: return True
 6             if d[i] == -1: return False
 7             d[i] = -1
 8             for neig in graph[i]:
 9                 if not dfs(d, graph, neig):
10                     return False
11             d[i] = 1
12             ans.append(i)
13             return True
14         for i in range(n):
15             if not dfs(d, graph, i):
16                 return []
17         return ans[::-1]   # remember to reverse the ans

 

转载于:https://www.cnblogs.com/Johnsonxiong/p/9279415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化该代码class Path(object): def __init__(self,path,cost1,cost2): self.__path = path self.__cost1 = cost1 self.__cost2 = cost2 #路径上最后一个节点 def getLastNode(self): return self.__path[-1] #获取路径路径 @property def path(self): return self.__path #判断node是否为路径上最后一个节点 def isLastNode(self, node): return node == self.getLastNode() #增加加点和成本产生一个新的path对象 def addNode(self, node, price1,price2): return Path(self.__path+[node],self.__cost1+ price1,self.__cost2+ price2) #输出当前路径 def printPath(self): global num #将num作为循环次数,即红绿灯数量 global distance num = 0 for n in self.__path: if self.isLastNode(node=n): print(n) else: print(n, end="->") num += 1 print("全程约为 {:.4}公里".format(str(self.__cost1))) print("时间大约为 {}分钟".format(str(self.__cost2))) print("需要经过{}个红绿灯".format(num)) distance = self.__cost1 #获取路径总成本的只读属性 @property def travelCost1(self): return self.__cost1 @property def travelCost2(self): return self.__cost2 class DirectedGraph(object): def __init__(self, d): if isinstance(d, dict): self.__graph = d else: self.__graph = dict() print('Sth error') def __generatePath(self, graph, path, end, results): #current = path[-1] current = path.getLastNode() if current == end: results.append(path) else: for n in graph[current]: #if n not in path: if n not in path.path: #self.__generatePath(graph, path + [n], end, results) self.__generatePath(graph, path.addNode(n,self.__graph[path.getLastNode()][n][0],self.__graph[path.getLastNode()][n][1]),end, results) #self.__generatePath(graph,使其能够保存输入记录并且能够查询和显示
06-13

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值