-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
74 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
* Execution: python edge_weighted_directed_cycle.py V E F | ||
* Data files: https://algs4.cs.princeton.edu/43mst/tinyEWG.txt | ||
* https://algs4.cs.princeton.edu/43mst/mediumEWG.txt | ||
* https://algs4.cs.princeton.edu/43mst/largeEWG.txt | ||
* | ||
* Finds a directed cycle in an edge-weighted digraph. | ||
* Runs in O(E + V) time.. | ||
* | ||
* | ||
""" | ||
from algs4.bag import Bag | ||
from algs4.directed_edge import DirectedEdge | ||
|
||
|
||
class EdgeWeightedDirectedCycle: | ||
def __init__(self, v=0, **kwargs): | ||
self.V = v | ||
self.E = 0 | ||
self.adj = {} | ||
for v in range(self.V): | ||
self.adj[v] = Bag() | ||
|
||
if 'file' in kwargs: | ||
# init a digraph by a file input | ||
in_file = kwargs['file'] | ||
self.V = int(in_file.readline()) | ||
for v in range(self.V): | ||
self.adj[v] = Bag() | ||
E = int(in_file.readline()) | ||
for i in range(E): | ||
v, w, weight = in_file.readline().split() | ||
self.add_edge(DirectedEdge(int(v), int(w), float(weight))) | ||
|
||
def __str__(self): | ||
s = "%d vertices, %d edges\n" % (self.V, self.E) | ||
for i in range(self.V): | ||
adjs = " ".join([str(x) for x in self.adj[i]]) | ||
s += "%d: %s\n" % (i, adjs) | ||
return s | ||
|
||
def add_edge(self, e): | ||
self.adj[e.From()].add(e) | ||
self.E += 1 | ||
|
||
def edges(self): | ||
edges = [] | ||
for v in range(self.V): | ||
for e in self.adj[v]: | ||
edges.append(e) | ||
return edges | ||
|
||
|
||
if __name__ == "__main__": | ||
import sys | ||
V, E, F = sys.argv[1:] | ||
graph = EdgeWeightedDirectedCycle(int(V), int(E), int(F)) | ||
|
||
print(graph) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,26 +11,25 @@ | |
|
||
here = path.abspath(path.dirname(__file__)) | ||
|
||
with open(path.join(here, 'README.md'), encoding='utf-8') as f: | ||
with open(path.join(here, "README.md"), encoding="utf-8") as f: | ||
long_description = f.read() | ||
|
||
setup( | ||
name='algs4', | ||
version='1.0.1', | ||
description='A Python implementation library for book algs4', | ||
name="algs4", | ||
version="1.0.2", | ||
description="A Python implementation library for book algs4", | ||
long_description=long_description, | ||
long_description_content_type='text/markdown', | ||
url='https://github.com/shellfly/algs4-py', | ||
author='shellfly', | ||
author_email='[email protected]', | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/shellfly/algs4-py", | ||
author="shellfly", | ||
author_email="[email protected]", | ||
classifiers=[ | ||
'Development Status :: 4 - Beta', | ||
'Intended Audience :: Developers', | ||
'Topic :: Software Development :: Build Tools', | ||
'License :: OSI Approved :: MIT License', | ||
'Programming Language :: Python :: 3', | ||
"Development Status :: 4 - Beta", | ||
"Intended Audience :: Developers", | ||
"Topic :: Software Development :: Build Tools", | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python :: 3", | ||
], | ||
|
||
keywords='algs4 algorithm', # Optional | ||
packages=['algs4'] | ||
keywords="algs4 algorithm", # Optional | ||
packages=["algs4", "algs4.utils"], | ||
) |