Skip to content

Commit

Permalink
add comment for breadth first paths
Browse files Browse the repository at this point in the history
  • Loading branch information
shellfly committed Feb 2, 2020
1 parent de38800 commit 22c82f1
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions algs4/breadth_first_paths.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
"""
Execution: python breadth_first_paths.py G s
Data files: https://algs4.cs.princeton.edu/41graph/tinyCG.txt
https://algs4.cs.princeton.edu/41graph/tinyG.txt
https://algs4.cs.princeton.edu/41graph/mediumG.txt
https://algs4.cs.princeton.edu/41graph/largeG.txt
Run breadth first search on an undirected graph.
Runs in O(E + V) time.
% python grapy.py tinyCG.txt
6 8
0: 2 1 5
1: 0 2
2: 0 1 3 4
3: 5 4 2
4: 3 2
5: 3 0
% python breadth_first_paths.py tinyCG.txt 0
0 to 0: 0
0 to 1: 0-2-1
0 to 2: 0-2
0 to 3: 0-2-3
0 to 4: 0-2-3-4
0 to 5: 0-2-3-5
* Execution python bread_first_paths.py G s
* Dependencies: Graph.java Queue.java Stack.java StdOut.java
* Data files: https://algs4.cs.princeton.edu/41graph/tinyCG.txt
* https://algs4.cs.princeton.edu/41graph/tinyG.txt
* https://algs4.cs.princeton.edu/41graph/mediumG.txt
* https://algs4.cs.princeton.edu/41graph/largeG.txt
*
* Run breadth first search on an undirected graph.
* Runs in O(E + V) time.
*
* % python graph.py tinyCG.txt
* 6 8
* 0: 2 1 5
* 1: 0 2
* 2: 0 1 3 4
* 3: 5 4 2
* 4: 3 2
* 5: 3 0
*
* % python bread_first_paths.py tinyCG.txt 0
* 0 to 0 (0): 0
* 0 to 1 (1): 0-1
* 0 to 2 (1): 0-2
* 0 to 3 (2): 0-2-3
* 0 to 4 (2): 0-2-4
* 0 to 5 (1): 0-5
*
* % java BreadthFirstPaths largeG.txt 0
* 0 to 0 (0): 0
* 0 to 1 (418): 0-932942-474885-82707-879889-971961-...
* 0 to 2 (323): 0-460790-53370-594358-780059-287921-...
* 0 to 3 (168): 0-713461-75230-953125-568284-350405-...
* 0 to 4 (144): 0-460790-53370-310931-440226-380102-...
* 0 to 5 (566): 0-932942-474885-82707-879889-971961-...
* 0 to 6 (349): 0-932942-474885-82707-879889-971961-...
*
"""

from algs4.stack import Stack
from algs4.queue import Queue
from algs4.graph import Graph
Expand Down Expand Up @@ -66,6 +76,7 @@ def path_to(self, v):
path.push(self.s)
return path


if __name__ == '__main__':
import sys
f = open(sys.argv[1])
Expand Down

0 comments on commit 22c82f1

Please sign in to comment.