Questions tagged [graph-algorithm]

Graph Algorithms are a sequence of well-defined steps that will solve a problem related to Graph Theory, where a Graph in this context is a collection of vertices ("nodes") and edges that connect these vertices.

What are Graphs? And what are Graph Algorithms?

Graph Theory is the study of mathematical structures that model objects and the relations between objects. These structures, known as graphs, are collections of vertices, or nodes, and edges which join them together.

There are many variations on graph structures and properties:

  • some have weighted edges (weighted graphs)
  • some have directed edges (directed graphs)
  • some have no cycles in their paths (acyclic graphs)
  • some have a very dense or sparse amount of edges
  • some are a combination of some or all of this properties/structures.

A Graph Algorithm is a sequence of well-defined steps that solve a problem regarding these mathematical structures, such as:

2782 questions
241
votes
3 answers

What are the differences between segment trees, interval trees, binary indexed trees and range trees?

What are differences between segment trees, interval trees, binary indexed trees and range trees in terms of: Key idea/definition Applications Performance/order in higher dimensions/space consumption Please do not just give definitions.
Aditya
  • 5,509
  • 4
  • 31
  • 51
228
votes
17 answers

Finding all cycles in a directed graph

How can I find (iterate over) ALL the cycles in a directed graph from/to a given node? For example, I want something like this: A->B->A A->B->C->A but not: B->C->B
user7305
  • 5,741
  • 9
  • 28
  • 23
131
votes
9 answers

Negative weights using Dijkstra's Algorithm

I am trying to understand why Dijkstra's algorithm will not work with negative weights. Reading an example on Shortest Paths, I am trying to figure out the following scenario: 2 A-------B \ / 3 \ / -2 \ / C From the…
Meir
  • 1,691
  • 2
  • 14
  • 15
108
votes
3 answers

Why does Dijkstra's algorithm use decrease-key?

Dijkstra's algorithm was taught to me was as follows while pqueue is not empty: distance, node = pqueue.delete_min() if node has been visited: continue else: mark node as visited if node == target: break …
weeb
  • 1,939
  • 4
  • 18
  • 29
89
votes
4 answers

Comparing object graph representation to adjacency list and matrix representations

I'm currently following Steve Yegge's advice on preparing for a technical programming interview: http://steve-yegge.blogspot.com/2008/03/get-that-job-at-google.html In his section on Graphs, he states: There are three basic ways to represent a…
jbeard4
  • 12,664
  • 4
  • 57
  • 67
72
votes
9 answers

A* Algorithm for very large graphs, any thoughts on caching shortcuts?

I'm writing a courier/logistics simulation on OpenStreetMap maps and have realised that the basic A* algorithm as pictured below is not going to be fast enough for large maps (like Greater London). The green nodes correspond to ones that were put…
drspa44
  • 1,041
  • 8
  • 12
58
votes
8 answers

Relaxation of an edge in Dijkstra's algorithm

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.
Geek
  • 26,489
  • 43
  • 149
  • 227
53
votes
11 answers

How to keep track of depth in breadth first search?

I have a tree as input to the breadth first search and I want to know as the algorithm progresses at which level it is? # Breadth First Search Implementation graph = { 'A':['B','C','D'], 'B':['A'], 'C':['A','E','F'], …
jsonbourne
  • 915
  • 2
  • 10
  • 17
47
votes
3 answers

Construct a minimum spanning tree covering a specific subset of the vertices

I have an undirected, positive-edge-weight graph (V,E) for which I want a minimum spanning tree covering a subset k of vertices V (the Steiner tree problem). I'm not limiting the size of the spanning tree to k vertices; rather I know exactly which k…
atp
  • 30,132
  • 47
  • 125
  • 187
47
votes
4 answers

What is the problem name for Traveling salesman problem(TSP) without considering going back to starting point?

I would like to know what is the problem name for TSP w/o considering the way of going back to starting point and what is the algorithm to solve this. I looked into Shortest path problem but that is not what I am looking for, the problem only find…
A-letubby
  • 8,474
  • 8
  • 38
  • 48
46
votes
4 answers

Graph serialization

I'm looking for a simple algorithm to 'serialize' a directed graph. In particular I've got a set of files with interdependencies on their execution order, and I want to find the correct order at compile time. I know it must be a fairly common thing…
Kieron
  • 11,588
  • 5
  • 34
  • 29
37
votes
1 answer

Algorithm for finding a Hamiltonian Path in a DAG

I am referring to Skienna's Book on Algorithms. The problem of testing whether a graph G contains a Hamiltonian path is NP-hard, where a Hamiltonian path P is a path that visits each vertex exactly once. There does not have to be an edge in G from…
34
votes
5 answers

String analysis

Given a sequence of operations: a*b*a*b*a*a*b*a*b is there a way to get the optimal subdivision to enable reusage of substring. making a*b*a*b*a*a*b*a*b => c*a*c, where c = a*b*a*b and then seeing that a*b*a*b => d*d, where d = a*b all in…
34
votes
4 answers

Grouping numbers based on occurrences?

Given the following three sequences of numbers, I would like to figure out how to group the numbers to find the closest relations between them. 1,2,3,4 4,3,5 2,1,3 ... I'm not sure what the algorithm(s) I'm looking for are called, but we can see…
Xeoncross
  • 55,620
  • 80
  • 262
  • 364
34
votes
4 answers

Explanation of Algorithm for finding articulation points or cut vertices of a graph

I have searched the net and could not find any explanation of a DFS algorithm for finding all articulation vertices of a graph. There is not even a wiki page. From reading around, I got to know the basic facts from here. PDF There is a variable at…
1
2 3
99 100