Questions tagged [graphs]

Questions about graphs, discrete structures of nodes which are connected by edges, including trees and graphs with weighted edges.

Questions about graphs, discrete structures of nodes which are connected by edges. Popular flavors are trees and networks with edge capacity.

5081 questions
87
votes
8 answers

Graph searching: Breadth-first vs. depth-first

When searching graphs, there are two easy algorithms: breadth-first and depth-first (Usually done by adding all adjactent graph nodes to a queue (breadth-first) or stack (depth-first)). Now, are there any advantages of one over another? The ones I…
malexmave
  • 995
  • 1
  • 7
  • 9
62
votes
5 answers

Is zero allowed as an edge's weight, in a weighted graph?

I am trying to write a script that generates random graphs and I need to know if an edge in a weighted graph can have the 0 value. actually it makes sense that 0 could be used as an edge's weight, but I've been working with graphs in last few days…
Taxellool
  • 739
  • 1
  • 5
  • 6
50
votes
3 answers

Why does Dijkstra's algorithm fail on a negative weighted graphs?

I know this is probably very basic, I just can't wrap my head around it. We recently studied about Dijkstra's algorithm for finding the shortest path between two vertices on a weighted graph. My professor said this algorithm will not work on a graph…
so.very.tired
  • 1,259
  • 1
  • 15
  • 20
49
votes
4 answers

Longest path in an undirected tree with only one traversal

There is this standard algorithm for finding longest path in undirected trees using two depth-first searches: Start DFS from a random vertex $v$ and find the farthest vertex from it; say it is $v'$. Now start a DFS from $v'$ to find the vertex…
e_noether
  • 1,329
  • 2
  • 13
  • 19
44
votes
1 answer

Do you get DFS if you change the queue to a stack in a BFS implementation?

Here is the standard pseudocode for breadth first search: { seen(x) is false for all x at this point } push(q, x0) seen(x0) := true while (!empty(q)) x := pop(q) visit(x) for each y reachable from x by one edge if not seen(y) push(q,…
rgrig
  • 1,346
  • 1
  • 11
  • 15
43
votes
9 answers

Algorithm to find diameter of a tree using BFS/DFS. Why does it work?

This link provides an algorithm for finding the diameter of an undirected tree using BFS/DFS. Summarizing: Run BFS on any node s in the graph, remembering the node u discovered last. Run BFS from u remembering the node v discovered last. d(u,v) is…
curryage
  • 541
  • 2
  • 7
  • 8
40
votes
4 answers

Algorithm that finds the number of simple paths from $s$ to $t$ in $G$

Can anyone suggest me a linear time algorithm that takes as input a directed acyclic graph $G=(V,E)$ and two vertices $s$ and $t$ and returns the number of simple paths from $s$ to $t$ in $G$. I have an algorithm in which I will run a DFS(Depth…
Saurabh
  • 929
  • 2
  • 10
  • 15
40
votes
1 answer

Finding an $st$-path in a planar graph which is adjacent to the fewest number of faces

I am curious whether the following problems has been studied before, but wasn't able to find any papers about it: Given a planar graph $G$, and two vertices $s$ and $t$, find an $s$-$t$ path $P$ which minimizes the number of distinct faces of…
Joe
  • 401
  • 3
  • 2
39
votes
14 answers

What are some real world applications of graphs?

Can you give some real world examples of what graphs algorithms people are actually using in applications? Given a complicated graphs, say social networks, what properties/quantity people want to know about them? —- It would be great if you can give…
LeafGlowPath
  • 568
  • 1
  • 4
  • 11
35
votes
5 answers

Enumerate all non-isomorphic graphs of a certain size

I'd like to enumerate all undirected graphs of size $n$, but I only need one instance of each isomorphism class. In other words, I want to enumerate all non-isomorphic (undirected) graphs on $n$ vertices. How can I do this? More precisely, I want…
D.W.
  • 167,959
  • 22
  • 232
  • 500
31
votes
1 answer

How hard is counting the number of simple paths between two nodes in a directed graph?

There is an easy polynomial algorithm to decide whether there is a path between two nodes in a directed graph (just do a routine graph traversal with, say, depth-first-search). However it seems that, surprisingly, the problem gets much harder if…
hugomg
  • 1,409
  • 1
  • 10
  • 15
31
votes
4 answers

The time complexity of finding the diameter of a graph

What is the time complexity of finding the diameter of a graph $G=(V,E)$? ${O}(|V|^2)$ ${O}(|V|^2+|V| \cdot |E|)$ ${O}(|V|^2\cdot |E|)$ ${O}(|V|\cdot |E|^2)$ The diameter of a graph $G$ is the maximum of the set of shortest path distances…
Gigili
  • 2,213
  • 3
  • 22
  • 31
31
votes
2 answers

Is Dijkstra's algorithm just BFS with a priority queue?

According to this page, Dijkstra's algorithm is just BFS with a priority queue. Is it really that simple? I think not.
Barry Fruitman
  • 755
  • 3
  • 10
  • 17
30
votes
1 answer

Graph problem known to be $NP$-complete only under Cook reduction

The theory of NP-completeness was initially built on Cook (polynomial-time Turing) reductions. Later, Karp introduced polynomial-time many-to-one reductions. A Cook reduction is more powerful than a Karp reduction since there is no restriction on…
Mohammad Al-Turkistany
  • 4,477
  • 1
  • 28
  • 37
29
votes
3 answers

Retrieving the shortest path of a dynamic graph

I'm studying shortest paths in directed graphs currently. There are many efficient algorithms for finding the shortest path in a network, like dijkstra's or bellman-ford's. But what if the graph is dynamic? By saying dynamic I mean that we can…
1
2 3
99 100