graph-algorithm

Find the edge which is not a part of any possible diameter of a tree

自古美人都是妖i 提交于 2019-12-11 16:55:55
问题 I'm curious if there is a quick way to find if an edge exists which is not a part of any possible diameter of a n-ary tree. For example in the following tree, A-B edge will not be a part of any diameter. I tried by listing down all the possible diameters, but that takes a lot of time and I'm certain that there is a faster way. 回答1: Let's begin with a simpler question: how would we find any diameter of the tree? One way to do this would be to pick some node and to root the tree at that node. A

Kosaraju's Algorithm for finding SCCs but keep track of edge between SCCs?

霸气de小男生 提交于 2019-12-11 15:04:26
问题 I currently have a working implementation of Kosaraji's algorithm that, given a directed graph with no weights, will print the SCCs in a graph. I would like to adapt it so it will also state where the edges between the SCCs are. Here is the code: from collections import defaultdict #---- Definitions ----# #Graph Graph = {} #Transpose of Graph Transpose_Graph = {} #Visited Nodes for Graph Visited_Nodes_Graph = {} #Visited Nodes for Transpose Graph Visited_Nodes_Transpose_Graph = {} #Stack to

How can I use BFS to get a path containing some given nodes in order? [closed]

天大地大妈咪最大 提交于 2019-12-11 14:48:10
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I have a graph with unweighted edges where each node is marked with a letter 'a' to 'z'. I want to modify the BFS algorithm to get the shortest path that has contains the letters 'c','o','d','e' in that order. There could be other letters between those four. You have starting node

Keeping track of the path, Knights travel

我的未来我决定 提交于 2019-12-11 14:15:04
问题 So far my shortest path method will stop when it reaches the goal position, printing out everything it did along the way. I would like to know how I could go about implementing parent positions so I can print the path along with the goal. This is an assignment. class Knight attr_accessor :x, :y, :prev_position, :moves def initialize(position) @x = position[0] @y = position[1] @prev_position = nil @moves = [ [-1,-2], [-2,-1], [-2,+1], [-1,+2], [+1,-2], [+2,-1], [+2,+1], [+1,+2]] end def

Lengauer Tarjan Algorithm in BGL (boost graph library)

白昼怎懂夜的黑 提交于 2019-12-11 12:13:03
问题 I need to create a dominator tree for a given graph. The code I have compiles and runs without errors, but the output looks exactly the same as the input. I have defined the following type for my graph (to be analyzed) typedef boost::adjacency_list<boost::listS, boost::vecS, boost::bidirectionalS, vertex_info, edge_info> Graph; and I would like to have another object containing the corresponding dominator tree. I tried the following: // dominator tree is an empty Graph object dominator_tree =

Finding unique loops in the closed graph

亡梦爱人 提交于 2019-12-11 10:41:44
问题 I finally managed to write a code to identify all the loops possible with the current configuration. For example, for the image below, the following is the input to my program. network2=Pipes.NetworkManager(vertices=[1,2,3,4], nodes=[(1,2),(1,3),(1,4),(2,1),(2,3), (2,4),(3,1),(3,2),(3,4),(4,1),(4,2),(4,3)]) network2.search_loop() Now, I have hard-time in filtering the data from my output to find the unique loop. This is the result: starting search from 1 --------------------------------------

Find Better Way to Create Paths in JavaScript for Maximum Captures

℡╲_俬逩灬. 提交于 2019-12-11 10:16:48
问题 I have been working on algorithm for a week at last I have successfully made an algorithm to create paths that captures opponent pieces, and then find a Maximum captures Path on 8x8 board. I have created an array of the following move captures. var moves = [[37 , 69] , [69 , 101] , [ 101 , 99] , [69 ,71], [ 69 , 67] , [67 , 99] , [99 , 101] , [71 ,103] ] Note: Can't capture downwards respective to black. Here I don't know the end point so that's why I can use Search Algorithms such as BFS,

What is the difference between Floyd-Warshall and matrix multiplication graph algorithms?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 09:26:40
问题 I have to solve the following problem: Write a program that, given a directed graph with costs and two vertices, finds a lowest cost walk between the given vertices, or prints a message if there are negative cost cycles in the graph. The program shall use the matrix multiplication algorithm. I implemented the matrix multiplication algorithm as it is defined: a pseudo-matrix multiplication, where addition is replaced by minimization and multiplication with addition. But by doing this, I ended

Can I use a trie that has a whole word on each node?

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:04:07
问题 I want to implement a trie to check for the validity of paths, so I would have a tree built that contains all the possible path constructions by breaking it down by directory. So something like /guest/friendsList/search would go from the root node to it's child guest , then guest's child friendsList , and then friendsList's child search . If search is a leaf node then my string /guest/friendsList/search would be considered valid. Is this something a trie would be useful for. All the

Find path with unsorted data

北城以北 提交于 2019-12-11 04:39:45
问题 I have this data: data = [ {"start": "MIA", "end": "FCA"}, {"start": "FCA", "end": "GVK"}, {"start": "GVK", "end": "LSD"} ] With this data, I need to find a path. In the above case, the path would be from MIA to FCA , then FCA to GVK and, finally, GVK to LSD . The path will never have branches and It never goes back to a point that It has already passed, no loops. As output, I just need to get the "end" point of each element of the data array: ["FCA", "GVK", "LSD"] So, that's what I've tried: