graph-algorithm

is there is a route from city a to city b in no more than x days?

旧街凉风 提交于 2019-12-29 08:01:52
问题 I was in a trading firm interview, I was asked this question, you are travelling accross the state in a buses, the buses can stop at any C possible cities and you need to find a way to go from city a to city b. There are total B buses, each of which travels between two cities. All buses travel on a daily bases, for example each bus x leaves some city c1 on day d1 and arrives at another city b1 on another day d2 (d2>d1). Assume that if you arrive at a city on day d, you can catch any bus

Finding All Cliques of an Undirected Graph

旧巷老猫 提交于 2019-12-25 18:24:25
问题 How can I list all cliques of an Undirected Graph ? (Not all maximal cliques, like the Bron-Kerbosch algorithm) 回答1: The optimal solution is like this because in a complete graph there are 2^n cliques. Consider all subsets of nodes using a recursive function. And for each subset if all the edges are present between nodes of the subset, add 1 to your counter: (This is almost a pseudocode in C++) int clique_counter = 0; int n; //number of nodes in graph //i imagine nodes are numbered from 1 to

Maximum weighted bipartite matching for two sets of vertices of drastically different sizes

邮差的信 提交于 2019-12-25 01:46:04
问题 The abstract problem I want to find the best maximum matching in a complete weighted bipartite graph where the two sets of vertices differ drastically in size, i.e. one set of vertices is very large and the other one very small. The Hungarian algorithm is not a good approach for this problem since it adds dummy vertices to the smaller set such that the two sets have the same size, so I lose all the potential efficiency gains from one of the vertex sets being only very small. More concretely I

Is there a solution for this traversal problem?

夙愿已清 提交于 2019-12-24 06:37:41
问题 Say we have a matrix M x N ,given two randomly specified exit and entrance , find a path(up,down,left,right) from entrance to exit that covers each node in the matrix only once? 回答1: This is obviously not always solvable. Say you have this matrix, where A is entrance and B exit: +---+---+ | A | | +---+---+ | | B | +---+---+ How do you solve this? 回答2: One thing you could try is something like this: Split your matrix in two such that entrance and exit are in different partitions. Then, for

Find all paths in directed cyclic graph as regular expression

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 00:45:03
问题 Let G = (V,E,r) a rooted directed graph, defined by a set of vertices V and a set of edges E with a designated root node r. The graph may contain cycles. The task: Given two vertices x and y from V, find all paths from x to y. Since cycles are allowed, the set of paths may obviously be infinite. Therefore, I want to find the set of paths in the form of a regular expression (Kleene Algebra). Here are a few examples: Examples graphs. Multiplication means sequence, so a path abc means first a,

Is there any other Data structure to represent Graph other than Adjacency List or Adjacency Matrix?

删除回忆录丶 提交于 2019-12-23 12:58:37
问题 I was looking for different Data structures for representing Graph and I came accross Nvidia CUDA Toolkit and found out new way to represent graph with the help of source_indices, destination_offsets. Fascinated by this innovative representation of graph, I searched out for other ways of representing Graphs. But not found anything new. I was wondering if there was any other way to represent Graph other than Adjacency Matrix or Lists... 回答1: I was wondering if there was any other way to

Markov Clustering

≯℡__Kan透↙ 提交于 2019-12-23 10:58:08
问题 I have two questions to be precise. Firstly, I would like to know if there is an easy way to adapt the Markov Clustering Algorithm so that I can specify in advance, how many clusters I would like to have at the end. If not, which similiar algorithm would you recommend? And secondly how should be dealt with overlapping clusters in the Markov world? 回答1: 1). There is no easy way to adapt the MCL algorithm (note: its name is 'Markov cluster algorithm' without the 'ing'. Many people verbalise it

Non-recursive version of Tarjan's algorithm

∥☆過路亽.° 提交于 2019-12-23 05:43:29
问题 I have the following (recursive) implementation of Tarjan's algorithm to find strongly connected components in a graph and it works fine: public class StronglyConnectedComponents { public static List<List<int>> Search(Graph graph) { StronglyConnectedComponents scc = new StronglyConnectedComponents(); return scc.Tarjan(graph); } private int preCount; private int[] low; private bool[] visited; private Graph graph; private List<List<int>> stronglyConnectedComponents = new List<List<int>>();

Minimum path between two vertices passing through a given set

半腔热情 提交于 2019-12-23 03:57:08
问题 Suppose I have a source node S , destination node D and a set A of intermediate nodes P1,P2, P3,… in an edge-weighted undirected graph. I want to find the vertex Pi ∈ A that minimizes dist(S,Pi)+dist(D,Pi) ? In addition, the overall path from S to D should contain only one node from the set A . What is an efficient algorithm for this? I don't want to go with brute-force approach. 回答1: What do you mean by brute force? Without assumption If you remove the assumption about "only one node from

Directed graph linear algorithm

有些话、适合烂在心里 提交于 2019-12-23 02:30:49
问题 I would like to know the best way to calculate the length of the shortest path between vertex s and every other vertex of the graph in linear time using dynamic programming. The graph is weighted DAG. 回答1: What you can hope for is an algorithm linear in the number of edges and vertices, i.e. O(|E| + |V|) , which also works correctly in presence of negative weights. This is done by first computing a topological order and then 'exploring' the graph in the order given by this topological order.