graph-theory

Shortest two disjoint paths between two specified vertices

女生的网名这么多〃 提交于 2019-11-29 15:00:50
问题 Given a weighted undirected graph G and two vertices a, b , we want to find two paths a -> b and b -> a such that they don't share any edge, and such that the sum of weights of edges in both paths is minimum. There can be up to 1,000 vertices, and up to 10,000 edges. I had initially tried to come up with a dynamic programming approach, but couldn't find such. Any ideas/suggestions would be extremely appreciated. 回答1: This is Minimum-cost flow problem. You can assign flow capacity for each

BFS traversal of all paths in graph using adjacency list

坚强是说给别人听的谎言 提交于 2019-11-29 12:10:43
I am currently trying to traverse all paths from source to destination in a graph which uses adjacency matrix. I have been trying to do it in BFS way.Thanks for the help. I am getting only one path. How do I get to print other paths as well ? public class AllPossiblePaths { static int v; static ArrayList<Integer> adj[]; public AllPossiblePaths(int v) { this.v = v; adj = new ArrayList[v]; for (int i = 0; i < v; i++) { adj[i] = new ArrayList<>(); } } // add edge from u to v public static void addEdge(int u, int v) { adj[u].add(v); } public static void findpaths(int source, int destination) {

Relaxation of an edge in Dijkstra's algorithm

左心房为你撑大大i 提交于 2019-11-29 10:35:51
问题 What does relaxation of an edge mean in the context of graph theory ? I came across this while studying up on Dijkstra's algorithm for single source shortest path. 回答1: Here's a nice description of the Algorithm that also explains the notion of relaxation. The notion of "relaxation" comes from an analogy between the estimate of the shortest path and the length of a helical tension spring, which is not designed for compression. Initially, the cost of the shortest path is an overestimate,

Touching segments

我是研究僧i 提交于 2019-11-29 09:56:14
问题 Can anyone please suggest me algorithm for this. You are given starting and the ending points of N segments over the x-axis. How many of these segments can be touched, even on their edges, by exactly two lines perpendicular to them? Sample Input : 3 5 2 3 1 3 1 5 3 4 4 5 5 1 2 1 3 2 3 1 4 1 5 3 1 2 3 4 5 6 Sample Output : Case 1: 5 Case 2: 5 Case 3: 2 Explanation : Case 1: We will draw two lines (parallel to Y-axis) crossing X-axis at point 2 and 4. These two lines will touch all the five

How do I find the shortest path that covers all nodes in a directed cyclic graph?

て烟熏妆下的殇ゞ 提交于 2019-11-29 07:29:36
I need an example of the shortest path of a directed cyclic graph from one node (it should reach to all nodes of the graph from a node that will be the input). Please if there is an example, I need it in C++, or the algorithm. EDIT: Oops, misread the question. Thanks @jfclavette for picking this up. Old answer is at the end. The problem you're trying to solve is called the Travelling salesman problem . There are many potential solutions , but it's NP-complete so you won't be able to solve for large graphs. Old answer: What you're trying to find is called the girth of a graph. It can be solved

Topological sort of cyclic graph with minimum number of violated edges

本秂侑毒 提交于 2019-11-29 07:11:00
问题 I am looking for a way to perform a topological sorting on a given directed unweighted graph, that contains cycles. The result should not only contain the ordering of vertices, but also the set of edges, that are violated by the given ordering. This set of edges shall be minimal. As my input graph is potentially large, I cannot use an exponential time algorithm. If it's impossible to compute an optimal solution in polynomial time, what heuristic would be reasonable for the given problem? 回答1:

Algorithm to find the total number of connected sets in a matrix

喜欢而已 提交于 2019-11-29 03:59:36
i wanted to know which algorithm should i apply here. Would a DFS do? Given a 2–d matrix. Find the total number of connected sets in that matrix. Connected set can be defined as group of cell(s) which has 1 mentioned on it and have at least one other cell in that set with which they share the neighbor relationship. A cell with 1 in it and no surrounding neighbor having 1 in it can be considered as a set with one cell in it. Neighbors can be defined as all the cells adjacent to the given cell in 8 possible directions (i.e. N, W, E, S, NE, NW, SE, SW direction). A cell is not a neighbor of

small cycle finding in a planar graph

你。 提交于 2019-11-29 02:08:30
I have a geometric undirected planar graph , that is a graph where each node has a location and no 2 edges cross, and I want to find all cycles that have no edges crossing them. Are there any good solutions known to this problem? What I'm planning on doing is a sort of A* like solution: insert every edge in a min heap as a path extend the shortest path with every option cull paths that loop back to other than there start (might not be needed) cull paths that would be the third to use ang given edge Does anyone see an issue with this? Will it even work? My first instinct is to use a method

How can I order a list of connections

我怕爱的太早我们不能终老 提交于 2019-11-29 02:04:37
I currently have a list of connections stored in a list where each connection is a directed link that connects two points and no point ever links to more than one point or is linked to by more than one point. For example: connections = [ (3, 7), (6, 5), (4, 6), (5, 3), (7, 8), (1, 2), (2, 1) ] Should produce: ordered = [ [ 4, 6, 5, 3, 7, 8 ], [ 1, 2, 1 ] ] I have attempt to do this using an algorithm that takes an input point and a list of connections and recursively calls itself to find the next point and add it to the growing ordered list. However, my algorithm breaks down when I don't start

Edge classification in a DFS

无人久伴 提交于 2019-11-29 01:03:22
问题 According to the book (Intro to Algorithm), in dfs, edges are classified as 4 kinds: Tree Edge, if in edge (u,v), v is first discovered, then (u, v) is a tree edge. Back Edge, if ......, v is discovered already and v is an ancestor, then it's a back edge. Forward Edge, if ......, v is discovered already and v is a descendant of u, forward edge it is. Cross Edge, all edges except for the above three. My question is how can I identify whether v is u's ancestor or descendant when I'm trying to