graph-algorithm

Finding shortest path with SPARQL query

走远了吗. 提交于 2019-12-09 06:27:24
问题 I am trying to understand the computational limitations of the SPARQL query, and I would like know how to write a query that will determine if there is a directed path between two objects. I know a way to do it for a path of a specific length: SELECT ?a ?b ?c ?d WHERE { ?a <http://graphtheory/hasNeighbor> ?b . ?b <http://graphtheory/hasNeighbor> ?c . ?c <http://graphtheory/hasNeighbor> ?d . FILTER (?a != ?c && ?b != ?d && ?a = <http://graphtheory/node/1> && ?d = <http://graphtheory/node/2>) }

Finding the minimum cycle path in a dynamically directed graph

杀马特。学长 韩版系。学妹 提交于 2019-12-08 20:24:55
问题 I recently came across this (Edit: Problem A) interesting problem from Spotify's hacker challenge earlier this year which involves determining the switching at train truck junctions to route a train back to it's starting point. The train must arrive facing the same direction it left and the train can never reverse on the tracks. As I understand it, the problem can be modeled as an undirected(?) graph where we must find the shortest cycle from a certain vertex, or detect that no such cycle

Python: how to optimize the count of all possible shortest paths?

两盒软妹~` 提交于 2019-12-08 12:37:12
问题 In a 3x3 network I want to be able to determine all the shortest paths between any two nodes. Then, for each node in the network, I want to compute how many shortest paths pass through one specific node. This requires using the nx.all_shortest_paths(G,source,target) function, which returns a generator . This is at variance from using the nx.all_pairs_shortest_path(G) , as suggested here. The difference is that in the former case the function computes all the shortest paths between any two

Circulation in network flow

泪湿孤枕 提交于 2019-12-08 10:50:16
问题 I am reading Algorithms book written by Robert Sedwick. Note: "s" is source and "t" is tank. Augument any flow network with an edge from "t" to "s" with flow and capacity equal to the network's value, and know that inflow is equal to outflow for any set of nodes in the augumented network. Such a flow is called a circulation, and this construction demonstrates that the maxflow problem reduces to the problem of finding a circulation that maximizes the flow along a given edge. Given a set of

How to iterate through a list of objects and assign children?

我的梦境 提交于 2019-12-08 08:56:03
问题 I have a Location POJO that stores Location objects parsed in from a JSON file, which I want to map to a graph. Each node's location in the graph corresponds to it's id field, where id="1" is the start node and id="10" is the goal node. To solve this I adapted a Node class to include methods such as setWeight() , addChildLocation() etc , But I'm not sure how to create the graph from my list of locations. I know how to create the graph by hard coding the location's and calling addChildren, by

Dijkstra's algorthm modification

ぐ巨炮叔叔 提交于 2019-12-08 08:53:57
问题 I am aware of the Dijkstra's shortest path algorithm. However, if I were to modify it so that instead of finding the shortest path it would find the longest path using a greedy algorithm. What would I have to do to the code below: Here is what Im using: as a compare function to select the correct node in the shortest path version: if (Cost(potential_node) > Cost(current_node) + cost(source , current_node)) then cost (potential_node) = cost(current_node) + cost (source, current_node) However,

Validating and normalizing a partially ordered set

青春壹個敷衍的年華 提交于 2019-12-08 08:12:55
问题 I have an array of pairs like this: [["a", "b"], ["b", "d"], ["a", "c"], ["e", "d"], ["a", "d"], ..., ["s", "f"]] What is an efficient way to check if the given array can express a partial ordering? That is, there is no "loop" in the given array like ["a", "b"], ["b", "c"], ["c", "a"] . If it is confirmed that the array expresses a partial order, I want to normalize this by removing all of the pairs that can be derived by reflexivity or transitivity. For example, in the above, since there is

Construct an Adjacency List from a List of edges?

℡╲_俬逩灬. 提交于 2019-12-08 07:50:31
问题 In context of graph algorithms, we are usually given a convenient representation of a graph (usually as an adjacency list or an adjacency matrix) to operate on. My question is, what is an efficient way to construct an Adjacency list from a given list of all edges ? For the purpose of the question, assume that edges are a list of tuples (as in python) and (a,b) denotes a directed edge from a to b. 回答1: A combination of itertools.groupby (docs), sorting and dict comprehension could get you

Correlation Network Implementation

感情迁移 提交于 2019-12-08 07:00:18
问题 I have been working on my graph/network problem, and I think I finally know what I want to do. Now that I am getting into the implementation, I am having issues deciding what libraries to use. The graph itself is pretty simple, each node is labeled by a string, and each each is a probability/correlation coefficient between the two nodes(variables), and is undirected. The operations that I want to perform on the graph are: Inserting new nodes/edges (fast) Finding the all pairs shortest (1

Find all tuples related to a certain string in Python

佐手、 提交于 2019-12-08 02:01:51
问题 I am trying to find all tuples related to a string, not just matched to it. Here is what I made: from itertools import chain data = [('A','B'),('B','C'),('B','D'),('B','F'),('F','W'),('W','H'),('G','Z')] init = 'A' filtered_init = [item for item in data if item[0] == init or item[1] == init] elements = list(dict.fromkeys([ i for i in chain(*filtered_init)])) elements.remove(init) dat = [] for i in elements: sync = [item for item in data if item[0] == i or item[1] == i] dat.append(sync) print