Questions tagged [graph-theory]

A graph is a mathematical structure that contains a collection of vertices or 'nodes' and a collection of edges that connect pairs of vertices. Graphs can be undirected or directed, where edges are directed, indicating from which vertex they start and to which they lead ( be it other or the same ).

Not to be confused with visual graphs (bar graphs, etc), a graph has a very specific meaning:

Graph G = (V,E), where V is a set of vertices or nodes, and E is a set of edges or arcs. The set of edges may be directed or undirected, which in turn makes G directed or undirected.

Example:

enter image description here

Graph theory is a subfield of mathematics and computer science dealing with the representation, implementation, and algorithmic design of graphs.

Wikipedia

4302 questions
466
votes
15 answers

What are the practical factors to consider when choosing between Depth-First Search (DFS) and Breadth-First Search (BFS)?

I understand the differences between DFS and BFS, but I'm interested to know what factors to consider when choosing DFS vs BFS. Things like avoiding DFS for very deep trees, etc.
Parth
  • 4,749
  • 3
  • 16
  • 4
451
votes
14 answers

Best algorithm for detecting cycles in a directed graph

Is there an efficient algorithm for detecting cycles within a directed graph? I have a directed graph representing a schedule of jobs that need to be executed, a job being a node and a dependency being an edge. I need to detect the error case of a…
Peauters
  • 4,773
  • 3
  • 18
  • 11
244
votes
10 answers

When should I use Kruskal as opposed to Prim (and vice versa)?

I was wondering when one should use Prim's algorithm and when Kruskal's to find the minimum spanning tree? They both have easy logics, same worst cases, and only difference is implementation which might involve a bit different data structures. So…
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
193
votes
13 answers

Why doesn't Dijkstra's algorithm work for negative weight edges?

Can somebody tell me why Dijkstra's algorithm for single source shortest path assumes that the edges must be non-negative. I am talking about only edges not the negative weight cycles.
Madu
  • 4,849
  • 9
  • 44
  • 78
193
votes
9 answers

Why is the time complexity of both DFS and BFS O( V + E )

The basic algorithm for BFS: set start vertex to visited load it into queue while queue not empty for each edge incident to vertex if its not visited load into queue mark vertex So I would think the time…
ordinary
  • 5,943
  • 14
  • 43
  • 60
121
votes
17 answers

Graph Algorithm To Find All Connections Between Two Arbitrary Vertices

I am trying to determine the best time efficient algorithm to accomplish the task described below. I have a set of records. For this set of records I have connection data which indicates how pairs of records from this set connect to one another.…
Robert Groves
  • 7,574
  • 6
  • 38
  • 50
113
votes
11 answers

Why DFS and not BFS for finding cycle in graphs

Predominantly DFS is used to find a cycle in graphs and not BFS. Any reasons? Both can find if a node has already been visited while traversing the tree/graph.
98
votes
11 answers

Find the shortest path in a graph which visits certain nodes

I have a undirected graph with about 100 nodes and about 200 edges. One node is labelled 'start', one is 'end', and there's about a dozen labelled 'mustpass'. I need to find the shortest path through this graph that starts at 'start', ends at…
Daniel
  • 2,032
  • 5
  • 21
  • 27
90
votes
11 answers

Find all paths between two graph nodes

I am working on an implementation of Dijkstra's Algorithm to retrieve the shortest path between interconnected nodes on a network of routes. I have the implementation working. It returns all the shortest paths to all the nodes when I pass the start…
Paul
  • 1,103
  • 1
  • 13
  • 18
90
votes
3 answers

How can I find the shortest path between 100 moving targets? (Live demo included.)

Background This picture illustrates the problem: I can control the red circle. The targets are the blue triangles. The black arrows indicate the direction that the targets will move. I want to collect all targets in the minimum number of…
Peter de Rivaz
  • 33,126
  • 4
  • 46
  • 75
79
votes
3 answers

How to prevent edges in graphviz to overlap each other

I have a graph I've created in graphviz, but the problem is that edges overlap each other (I have 5-7 nodes in each row), so it is hard to tell for each node which are the nodes it connects. How can I make the edges not to overlap each other? Have…
David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125
78
votes
6 answers

Visualizing Undirected Graph That's Too Large for GraphViz?

I need advice for rendering an undirected graph with 178,000 nodes and 500,000 edges. I've tried Neato, Tulip, and Cytoscape. Neato doesn't even come remotely close, and Tulip and Cytoscape claim they can handle it but don't seem to be able to. …
Gabe
76
votes
5 answers

How to draw a graph in LaTeX?

First of all, let me say I'm using LyX, though I have no problem using ERT. Secondly, what is the most simplest way to draw a simple graph like this in Latex? I've seen some documents with graphs and I've seen some examples, but I couldn't figure…
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
73
votes
7 answers

What is the most efficient graph data structure in Python?

I need to be able to manipulate a large (10^7 nodes) graph in python. The data corresponding to each node/edge is minimal, say, a small number of strings. What is the most efficient, in terms of memory and speed, way of doing this? A dict of dicts…
bgoncalves
  • 1,687
  • 3
  • 19
  • 19
1
2 3
99 100