Skip to content

Commit 54a920d

Browse files
committed
add missing files
1 parent e21b863 commit 54a920d

File tree

2 files changed

+74
-16
lines changed

2 files changed

+74
-16
lines changed

algs4/edge_weighted_directed_cycle.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
"""
2+
* Execution: python edge_weighted_directed_cycle.py V E F
3+
* Data files: https://algs4.cs.princeton.edu/43mst/tinyEWG.txt
4+
* https://algs4.cs.princeton.edu/43mst/mediumEWG.txt
5+
* https://algs4.cs.princeton.edu/43mst/largeEWG.txt
6+
*
7+
* Finds a directed cycle in an edge-weighted digraph.
8+
* Runs in O(E + V) time..
9+
*
10+
*
11+
"""
12+
from algs4.bag import Bag
13+
from algs4.directed_edge import DirectedEdge
14+
15+
16+
class EdgeWeightedDirectedCycle:
17+
def __init__(self, v=0, **kwargs):
18+
self.V = v
19+
self.E = 0
20+
self.adj = {}
21+
for v in range(self.V):
22+
self.adj[v] = Bag()
23+
24+
if 'file' in kwargs:
25+
# init a digraph by a file input
26+
in_file = kwargs['file']
27+
self.V = int(in_file.readline())
28+
for v in range(self.V):
29+
self.adj[v] = Bag()
30+
E = int(in_file.readline())
31+
for i in range(E):
32+
v, w, weight = in_file.readline().split()
33+
self.add_edge(DirectedEdge(int(v), int(w), float(weight)))
34+
35+
def __str__(self):
36+
s = "%d vertices, %d edges\n" % (self.V, self.E)
37+
for i in range(self.V):
38+
adjs = " ".join([str(x) for x in self.adj[i]])
39+
s += "%d: %s\n" % (i, adjs)
40+
return s
41+
42+
def add_edge(self, e):
43+
self.adj[e.From()].add(e)
44+
self.E += 1
45+
46+
def edges(self):
47+
edges = []
48+
for v in range(self.V):
49+
for e in self.adj[v]:
50+
edges.append(e)
51+
return edges
52+
53+
54+
if __name__ == "__main__":
55+
import sys
56+
V, E, F = sys.argv[1:]
57+
graph = EdgeWeightedDirectedCycle(int(V), int(E), int(F))
58+
59+
print(graph)

setup.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,25 @@
1111

1212
here = path.abspath(path.dirname(__file__))
1313

14-
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
14+
with open(path.join(here, "README.md"), encoding="utf-8") as f:
1515
long_description = f.read()
1616

1717
setup(
18-
name='algs4',
19-
version='1.0.1',
20-
description='A Python implementation library for book algs4',
18+
name="algs4",
19+
version="1.0.2",
20+
description="A Python implementation library for book algs4",
2121
long_description=long_description,
22-
long_description_content_type='text/markdown',
23-
url='https://github.com/shellfly/algs4-py',
24-
author='shellfly',
25-
author_email='[email protected]',
22+
long_description_content_type="text/markdown",
23+
url="https://github.com/shellfly/algs4-py",
24+
author="shellfly",
25+
author_email="[email protected]",
2626
classifiers=[
27-
'Development Status :: 4 - Beta',
28-
'Intended Audience :: Developers',
29-
'Topic :: Software Development :: Build Tools',
30-
'License :: OSI Approved :: MIT License',
31-
'Programming Language :: Python :: 3',
27+
"Development Status :: 4 - Beta",
28+
"Intended Audience :: Developers",
29+
"Topic :: Software Development :: Build Tools",
30+
"License :: OSI Approved :: MIT License",
31+
"Programming Language :: Python :: 3",
3232
],
33-
34-
keywords='algs4 algorithm', # Optional
35-
packages=['algs4']
33+
keywords="algs4 algorithm", # Optional
34+
packages=["algs4", "algs4.utils"],
3635
)

0 commit comments

Comments
 (0)