Repository: itu-algorithms/itu.algs4
Branch: master
Commit: cb08048fa718
Files: 133
Total size: 507.2 KB
Directory structure:
gitextract_mlb3nvxv/
├── .github/
│ └── workflows/
│ ├── check.yml
│ └── python_publish.yml
├── .gitignore
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── create_html_doc.sh
├── docs/
│ ├── conf.py
│ ├── index.rst
│ ├── requirements.txt
│ └── source/
│ ├── itu.algs4.errors.rst
│ ├── itu.algs4.fundamentals.rst
│ ├── itu.algs4.graphs.rst
│ ├── itu.algs4.rst
│ ├── itu.algs4.searching.rst
│ ├── itu.algs4.sorting.rst
│ ├── itu.algs4.stdlib.rst
│ └── itu.algs4.strings.rst
├── examples/
│ ├── bst.py
│ ├── hello_world.py
│ ├── queue.py
│ ├── sort-numbers.py
│ └── stack.py
├── itu/
│ ├── __init__.py
│ └── algs4/
│ ├── __init__.py
│ ├── errors/
│ │ ├── __init__.py
│ │ └── errors.py
│ ├── fundamentals/
│ │ ├── __init__.py
│ │ ├── bag.py
│ │ ├── binary_search.py
│ │ ├── evaluate.py
│ │ ├── java_helper.py
│ │ ├── queue.py
│ │ ├── stack.py
│ │ ├── three_sum.py
│ │ ├── three_sum_fast.py
│ │ ├── two_sum_fast.py
│ │ └── uf.py
│ ├── graphs/
│ │ ├── Arbitrage.py
│ │ ├── CPM.py
│ │ ├── __init__.py
│ │ ├── acyclic_lp.py
│ │ ├── acyclic_sp.py
│ │ ├── bellman_ford_sp.py
│ │ ├── bipartite.py
│ │ ├── breadth_first_paths.py
│ │ ├── cc.py
│ │ ├── cycle.py
│ │ ├── degrees_of_separation.py
│ │ ├── depth_first_order.py
│ │ ├── depth_first_paths.py
│ │ ├── depth_first_search.py
│ │ ├── digraph.py
│ │ ├── dijkstra_all_pairs_sp.py
│ │ ├── dijkstra_sp.py
│ │ ├── dijkstra_undirected_sp.py
│ │ ├── directed_cycle.py
│ │ ├── directed_dfs.py
│ │ ├── directed_edge.py
│ │ ├── edge.py
│ │ ├── edge_weighted_digraph.py
│ │ ├── edge_weighted_directed_cycle.py
│ │ ├── edge_weighted_directed_cycle_anton.py
│ │ ├── edge_weighted_graph.py
│ │ ├── graph.py
│ │ ├── kosaraju_sharir_scc.py
│ │ ├── kruskal_mst.py
│ │ ├── lazy_prim_mst.py
│ │ ├── prim_mst.py
│ │ ├── symbol_digraph.py
│ │ ├── symbol_graph.py
│ │ ├── topological.py
│ │ └── transitive_closure.py
│ ├── searching/
│ │ ├── __init__.py
│ │ ├── binary_search_st.py
│ │ ├── bst.py
│ │ ├── file_index.py
│ │ ├── frequency_counter.py
│ │ ├── linear_probing_hst.py
│ │ ├── lookup_csv.py
│ │ ├── lookup_index.py
│ │ ├── red_black_bst.py
│ │ ├── seperate_chaining_hst.py
│ │ ├── sequential_search_st.py
│ │ ├── set.py
│ │ ├── sparse_vector.py
│ │ └── st.py
│ ├── sorting/
│ │ ├── __init__.py
│ │ ├── datafiles/
│ │ │ ├── tiny.txt
│ │ │ └── words3.txt
│ │ ├── heap.py
│ │ ├── index_min_pq.py
│ │ ├── insertion_sort.py
│ │ ├── max_pq.py
│ │ ├── merge.py
│ │ ├── merge_bu.py
│ │ ├── min_pq.py
│ │ ├── quick3way.py
│ │ ├── quicksort.py
│ │ ├── selection.py
│ │ └── shellsort.py
│ ├── stdlib/
│ │ ├── __init__.py
│ │ ├── binary_out.py
│ │ ├── binary_stdin.py
│ │ ├── binary_stdout.py
│ │ ├── color.py
│ │ ├── instream.py
│ │ ├── outstream.py
│ │ ├── picture.py
│ │ ├── stdarray.py
│ │ ├── stdaudio.py
│ │ ├── stddraw.py
│ │ ├── stdio.py
│ │ ├── stdrandom.py
│ │ └── stdstats.py
│ └── strings/
│ ├── __init__.py
│ ├── boyer_moore.py
│ ├── huffman_compression.py
│ ├── kmp.py
│ ├── lsd.py
│ ├── lzw.py
│ ├── msd.py
│ ├── nfa.py
│ ├── quick3string.py
│ ├── rabin_karp.py
│ ├── trie_st.py
│ └── tst.py
├── setup.cfg
├── setup.py
└── tests/
├── test_bst.py
├── test_red_black_bst.py
├── test_stack.py
└── test_symbol_tables.py
================================================
FILE CONTENTS
================================================
================================================
FILE: .github/workflows/check.yml
================================================
name: check
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: ["3.10", "3.12"]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install
run: |
pip install --upgrade -e .[dev]
- name: Test
run: |
pytest --cov-report term-missing --cov itu.algs4
- name: Lint
run: |
flake8 examples tests itu
isort -c --diff examples tests itu
- name: Typecheck
run: |
mypy
continue-on-error: true
- name: Documentation
run: |
pip install sphinx
cd docs
mkdir _target && mkdir _static
sphinx-build . _target/
================================================
FILE: .github/workflows/python_publish.yml
================================================
name: Upload Python Package to PyPi
on:
push:
tags:
- "v*" # Push events matching v*, i.e. v1.0, v20.15.10
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
================================================
FILE: .gitignore
================================================
*.pyc
.vs/
algs4-data/
.mypy_cache
.pytest_cache
.ruff_cache
itu.algs4.egg-info
__pycache__
================================================
FILE: CONTRIBUTING.md
================================================
# Development
## Testing
Before you can run tests, you should clone the repository, and install the
package in "editable" mode, including its development dependencies:
```bash
pip install --upgrade -e '.[dev]'
```
Run all tests as follows:
```bash
pytest
```
To additionally display code coverage statistics, use this:
```bash
pytest --cov
```
To run individual tests, you can also do this:
```
python3 -m unittest tests/test_bst.py
pytest tests/test_stack.py
```
## Linter
Run `flake8` to lint all code. Run `black .` to automatically fix some linting error.
Moreover, run
```
isort -y
```
to sort import statements.
We enforce linting on [examples/](examples), [tests/](tests), and [itu/](itu).
## Types
Weak type checking is currently enforced only on [examples/](examples) and [tests/](tests). To run the type checker, try:
```
mypy
```
Ideally, we want every module to strictly type check. For example, the binary search trees strictly type check:
```
mypy --strict itu/algs4/searching/bst.py itu/algs4/searching/red_black_bst.py itu/algs4/fundamentals/queue.py
```
## Examples
Client code should be migrated to [examples/](examples).
## Uploading to PyPi
Create package and upload it:
```bash
python3 setup.py sdist bdist_wheel
python3 -m twine upload dist/*
```
## Useful Resources
- the book https://algs4.cs.princeton.edu/home/
- a python version of a similar book https://introcs.cs.princeton.edu/python/home/
- all java code -- good list, includes what needs to be done https://algs4.cs.princeton.edu/code/ https://github.com/kevin-wayne/algs4
## Coding style
https://www.python.org/dev/peps/pep-0008/#prescriptive-naming-conventions
- we have subdirectories for the code, one for each chapter
- if java relies on having different implementations depending on the type:
Use somehting like
```
class DirectedDFS:
def __init__(self, G, *s):
```
like in `graphs/directed_dfs.py`
Otherwise we use static factory methods where the name indicates the expected type.
If appropriate we use `isinstance()` and its variants, for example to distinguish undirected and directed graphs.
- things like 'node' are inside classes, no leading underscore
- file names, variables, methods are file_name (and not CamelCase, adjustting from algs4), only classes are CamelCase (PascalCase)
- there is one file per version of an algorithm / data structure (like in algs4), the name, and importantly the docstring, reflects which version it is
- java main becomes `__main__` stuff; follow what is there; adjust the initial comment
- don't replicate imports unless
- lower case letter with underscore
- like in the book
- private variables become _variable_name
- if java has `toString()`, then we have `__repr__()`
- keep the comments from the java code
- if in doubt, we go with the book, not the code on the book web site (keep it simple)
- docstring without formatting
## ideas
- should we include generators (additionally to iterators) everywhere?
================================================
FILE: LICENSE
================================================
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form. A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under section
7. This requirement modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has interactive
interfaces that do not display Appropriate Legal Notices, your
work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that product
model, to give anyone who possesses the object code either (1) a
copy of the Corresponding Source for all the software in the
product that is covered by this License, on a durable physical
medium customarily used for software interchange, for a price no
more than your reasonable cost of physically performing this
conveying of source, or (2) access to copy the
Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in accord
with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a charge), and offer equivalent access to the
Corresponding Source in the same way through the same place at no
further charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding Source
may be on a different server (operated by you or a third party)
that supports equivalent copying facilities, provided you maintain
clear directions next to the object code saying where to find the
Corresponding Source. Regardless of what server hosts the
Corresponding Source, you remain obligated to ensure that it is
available for as long as needed to satisfy these requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients. "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all. For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
Copyright (C)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
Copyright (C)
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
.
================================================
FILE: README.md
================================================
# Algs4 library for Python 3
`itu.algs4` is a Python 3 port of the Java code in [Algorithms, 4th Edition](https://algs4.cs.princeton.edu/home/).
[](https://github.com/itu-algorithms/itu.algs4/actions)
[](https://itualgs4.readthedocs.io/en/latest/?badge=latest)
## Target audience
`itu.algs4` is intended for instructors and students who wish to follow the textbook [Algorithms, 4th Edition](https://algs4.cs.princeton.edu/home/) by Sedgewick and Wayne.
It was first created in 2018 by teaching assistants and instructors at [ITU Copenhagen](https://algorithms.itu.dk), where the introductory course on Algorithms and Data Structures is taught bilingually in Java and Python 3.
## Installation
This library requires a functioning Python 3 environment; for example, the one provided by [Miniconda](https://www.anaconda.com/docs/getting-started/miniconda/main) or [Anaconda](https://www.anaconda.com/docs/getting-started/anaconda/main).
Some optional visual and auditory features depend on the [numpy](http://numpy.org) and [pygame](https://pygame.org) packages. These features are not used in the ITU course, and you shouldn't spend extra time on installing those packages unless you already have them or want to play around with the those parts on your own.
### With pip
If you have previously installed this package under its old name, we recommend you remove it with
```bash
pip uninstall algs4 algs4_python
```
Then you can install `itu.algs4` simply with
```bash
pip install itu.algs4
```
If you have already installed `itu.algs4` and want to upgrade to a new version, run:
```bash
pip install itu.algs4 --upgrade
```
To test that you have installed the library correctly, run this command:
```bash
python -c 'from itu.algs4.stdlib import stdio; stdio.write("Hello World!")'
```
It should greet you. If an error message appears instead, the library is not installed correctly.
### Alternative: With pip and git
If git is available, the following command will install the library in your Python environment:
```bash
pip install git+https://github.com/itu-algorithms/itu.algs4
```
### Alternative: With pip and zip
To install this library without git:
1. Download and unzip the repository.
2. Open a command prompt or terminal and navigate to the downloaded folder. There should be the file `setup.py`.
3. Use the command `pip3 install .` to install the package (this will also work for updating the package, when a newer version is available). If your Python installation is system-wide, use `sudo pip3 install .`
### Alternative: Step-by-step guide for Windows
To install the Python package `itu.algs4`:
- Download the repository by pressing the green "Clone or download" button, and pressing "Download ZIP".
- Extract the content of the zip to your Desktop (you can delete the folder after installing the package).
- Open the "Command Prompt" by pressing "Windows + R", type "cmd" in the window that appears, and press "OK".
- If you saved the folder on the Desktop you should be able to navigate to the folder by typing "cd Desktop\itu.algs4-master".
```
C:\Users\user>cd Desktop\itu.algs4-master
```
- When in the correct folder, type `pip install .` to install the package.
```
C:\Users\user\Desktop\itu.algs4-master>pip install .
```
- After this, the package should be installed correctly and you can delete the folder from your Desktop.
## Package structure
The Python package `itu.algs4` has a hierarchical structure with seven sub-packages:
- [itu.algs4.fundamentals](itu/algs4/fundamentals)
- [itu.algs4.sorting](itu/algs4/sorting)
- [itu.algs4.searching](itu/algs4/searching)
- [itu.algs4.graphs](itu/algs4/graphs)
- [itu.algs4.strings](itu/algs4/strings)
- [itu.algs4.stdlib](itu/algs4/stdlib)
- [itu.algs4.errors](itu/algs4/errors)
While deep nesting of packages is normally [discouraged](https://peps.python.org/pep-0423/#avoid-deep-nesting) in Python, an important design goal of `itu.algs4` was to mirror the structure of the original Java code.
The first five packages correspond to the first five chapters of [Algorithms, 4th Edition](https://algs4.cs.princeton.edu/home/). The `stdlib` package is based on the one from the related book [Introduction to Programming in Python](https://introcs.cs.princeton.edu/python/). The package `errors` contains some exception classes.
All filenames and package names have been written in lower_case style with underscores instead of the CamelCase style of the Java version. For example `EdgeWeightedDigraph.java` has been renamed to `edge_weighted_digraph.py`. Class names still use CamelCase though, which is consistent with naming conventions in Python.
## Examples
The directory [examples/](examples) contains examples, some of which are
described here.
### Hello World
A simple program, stored as a file [hello_world.py](examples/hello_world.py), looks like this:
```python
from itu.algs4.stdlib import stdio
stdio.write("Hello World!\n")
```
It can be run with the command `python hello_world.py`.
### Sort numbers
A slightly more interesting example is
[sort-numbers.py](examples/sort-numbers.py):
```python
from itu.algs4.sorting import merge
from itu.algs4.stdlib import stdio
"""
Reads a list of integers from standard input.
Then prints it in sorted order.
"""
L = stdio.readAllInts()
merge.sort(L)
if len(L) > 0:
stdio.write(L[0])
for i in range(1, len(L)):
stdio.write(" ")
stdio.write(L[i])
stdio.writeln()
```
This code uses the convenient function `stdio.readAllInts()` to read the
integers (separated by whitespaces) from the standard input and put them in the
array `L`. It then sorts the elements of the array. Finally, it outputs the
sorted list -- the code to do so is somewhat less elegant to get the whitespace
exactly right. (Of course, advanced Python users know more concise ways to
produce the same output: `print(" ".join(map(str, L)))`)
### Import classes
You can import classes, such as the class EdgeWeightedDigraph, with
```python
from itu.algs4.graphs.edge_weighted_digraph import EdgeWeightedDigraph
```
## Documentation
The documentation can be found [here](https://itualgs4.readthedocs.io/en/latest/).
You can use Python's built-in `help` function on any package, sub-package, public class, or function to get a description of what it contains or does. This documentation should also show up in your IDE of choice.
For example `help(itu.algs4)` yields the following:
```
Help on package itu.algs4 in itu:
NAME
itu.algs4
PACKAGE CONTENTS
errors (package)
fundamentals (package)
graphs (package)
searching (package)
sorting (package)
stdlib (package)
strings (package)
FILE
(built-in)
```
## Development
`itu.algs4` has known bugs and has not been tested systematically. We are open to pull requests, and in particular, we appreciate the contribution of high-quality test cases, bug-fixes, and coding style improvements. For more information, see the [CONTRIBUTING.md](CONTRIBUTING.md) file.
## Contributors
- Andreas Holck Høeg-Petersen
- Anton Mølbjerg Eskildsen
- Frederik Haagensen
- Holger Dell
- Martino Secchi
- Morten Keller Grøftehauge
- Morten Tychsen Clausen
- Nina Mesing Stausholm Nielsen
- Otto Stadel Clausen
- Riko Jacob
- Thore Husfeldt
- Viktor Shamal Andersen
## License
This project is licensed under the GPLv3 License - see the [LICENSE](LICENSE) file for details
## Links to other projects
- [algs4](https://github.com/kevin-wayne/algs4/) is the original Java implementation by Sedgewick and Wayne.
- The textbook [Introduction to Programming in Python](https://introcs.cs.princeton.edu/python/) by Sedgewick, Wayne, and Dondero has a somewhat different approach from [Algorithms, 4th Edition](https://algs4.cs.princeton.edu/home/), and is therefore not suitable for a bilingual course. Nevertheless, our code in [itu/algs4/stdlib/](itu/algs4/stdlib/) is largely based on the [source code](https://introcs.cs.princeton.edu/python/code/) associated with that book.
- [pyalgs](https://github.com/chen0040/pyalgs) is a Python port of `algs4` that uses a more idiomatic Python coding style. In contrast, our port tries to stay as close to the original Java library and the course book’s Java implementations as possible, so that it can be used with less friction in a bilingual course.
- [Scala-Algorithms](https://github.com/garyaiki/Scala-Algorithms) is a Scala port of `algs4`.
- [Algs4Net](https://github.com/nguyenqthai/Algs4Net) is a .NET port of `algs4`.
================================================
FILE: create_html_doc.sh
================================================
DIR=~/WorkSpace/bads-code/AlgorithmsInPython/algs4/
DEST=/home/rikj/WorkSpace/riko/html_templ_tum/itu_dest/AlgorithmsInPython
DIR=algs4
DEST=DOC
mkdir DOC
FILES=`cd $DIR; find . -type d -or -name \*.py`
cd $DEST
for f in $FILES
do
p=${f#./}
b=${p%.py}
if [ ! $b == ${b%datafiles} ]
then
continue
fi
if [ ! $b == ${b#test} ]
then
continue
fi
arg=algs4.`echo ${b} | tr / .`
res=`pydoc3 -w $arg | grep -v '^wrote'`
if [ ! -z "$res" ]
then
echo $arg $res
fi
# pydoc3 -w algs4.`echo ${b} | tr / .` > /dev/null
done
cd ../..
#rsync -va itu_dest/ ssh.itu.dk:public_html/
================================================
FILE: docs/conf.py
================================================
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath("../"))
# -- Project information -----------------------------------------------------
project = "itu.algs4"
copyright = "2020, ITU Algorithms Group"
author = "ITU Algorithms Group"
master_doc = "index"
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ["sphinx.ext.autodoc"]
# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = "alabaster"
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
================================================
FILE: docs/index.rst
================================================
.. itu.algs4 documentation master file, created by
sphinx-quickstart on Mon Jul 6 13:50:43 2020.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to itu.algs4's documentation!
=====================================
.. toctree::
:maxdepth: 2
:caption: Contents:
source/itu.algs4.fundamentals.rst
source/itu.algs4.graphs.rst
source/itu.algs4.searching.rst
source/itu.algs4.sorting.rst
source/itu.algs4.stdlib.rst
source/itu.algs4.strings.rst
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
================================================
FILE: docs/requirements.txt
================================================
pygame
typing_extensions
color
================================================
FILE: docs/source/itu.algs4.errors.rst
================================================
itu.algs4.errors package
========================
Submodules
----------
itu.algs4.errors.errors module
------------------------------
.. automodule:: itu.algs4.errors.errors
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: itu.algs4.errors
:members:
:undoc-members:
:show-inheritance:
================================================
FILE: docs/source/itu.algs4.fundamentals.rst
================================================
itu.algs4.fundamentals package
==============================
Submodules
----------
itu.algs4.fundamentals.bag module
---------------------------------
.. automodule:: itu.algs4.fundamentals.bag
:members:
:undoc-members:
:show-inheritance:
itu.algs4.fundamentals.binary\_search module
--------------------------------------------
.. automodule:: itu.algs4.fundamentals.binary_search
:members:
:undoc-members:
:show-inheritance:
itu.algs4.fundamentals.evaluate module
--------------------------------------
.. automodule:: itu.algs4.fundamentals.evaluate
:members:
:undoc-members:
:show-inheritance:
itu.algs4.fundamentals.java\_helper module
------------------------------------------
.. automodule:: itu.algs4.fundamentals.java_helper
:members:
:undoc-members:
:show-inheritance:
itu.algs4.fundamentals.queue module
-----------------------------------
.. automodule:: itu.algs4.fundamentals.queue
:members:
:undoc-members:
:show-inheritance:
itu.algs4.fundamentals.stack module
-----------------------------------
.. automodule:: itu.algs4.fundamentals.stack
:members:
:undoc-members:
:show-inheritance:
itu.algs4.fundamentals.three\_sum module
----------------------------------------
.. automodule:: itu.algs4.fundamentals.three_sum
:members:
:undoc-members:
:show-inheritance:
itu.algs4.fundamentals.three\_sum\_fast module
----------------------------------------------
.. automodule:: itu.algs4.fundamentals.three_sum_fast
:members:
:undoc-members:
:show-inheritance:
itu.algs4.fundamentals.two\_sum\_fast module
--------------------------------------------
.. automodule:: itu.algs4.fundamentals.two_sum_fast
:members:
:undoc-members:
:show-inheritance:
itu.algs4.fundamentals.uf module
--------------------------------
.. automodule:: itu.algs4.fundamentals.uf
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: itu.algs4.fundamentals
:members:
:undoc-members:
:show-inheritance:
================================================
FILE: docs/source/itu.algs4.graphs.rst
================================================
itu.algs4.graphs package
========================
Submodules
----------
itu.algs4.graphs.Arbitrage module
---------------------------------
.. automodule:: itu.algs4.graphs.Arbitrage
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.CPM module
---------------------------
.. automodule:: itu.algs4.graphs.CPM
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.acyclic\_lp module
-----------------------------------
.. automodule:: itu.algs4.graphs.acyclic_lp
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.acyclic\_sp module
-----------------------------------
.. automodule:: itu.algs4.graphs.acyclic_sp
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.bellman\_ford\_sp module
-----------------------------------------
.. automodule:: itu.algs4.graphs.bellman_ford_sp
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.bipartite module
---------------------------------
.. automodule:: itu.algs4.graphs.bipartite
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.breadth\_first\_paths module
---------------------------------------------
.. automodule:: itu.algs4.graphs.breadth_first_paths
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.cc module
--------------------------
.. automodule:: itu.algs4.graphs.cc
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.cycle module
-----------------------------
.. automodule:: itu.algs4.graphs.cycle
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.degrees\_of\_separation module
-----------------------------------------------
.. automodule:: itu.algs4.graphs.degrees_of_separation
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.depth\_first\_order module
-------------------------------------------
.. automodule:: itu.algs4.graphs.depth_first_order
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.depth\_first\_paths module
-------------------------------------------
.. automodule:: itu.algs4.graphs.depth_first_paths
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.depth\_first\_search module
--------------------------------------------
.. automodule:: itu.algs4.graphs.depth_first_search
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.digraph module
-------------------------------
.. automodule:: itu.algs4.graphs.digraph
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.dijkstra\_all\_pairs\_sp module
------------------------------------------------
.. automodule:: itu.algs4.graphs.dijkstra_all_pairs_sp
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.dijkstra\_sp module
------------------------------------
.. automodule:: itu.algs4.graphs.dijkstra_sp
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.dijkstra\_undirected\_sp module
------------------------------------------------
.. automodule:: itu.algs4.graphs.dijkstra_undirected_sp
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.directed\_cycle module
---------------------------------------
.. automodule:: itu.algs4.graphs.directed_cycle
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.directed\_dfs module
-------------------------------------
.. automodule:: itu.algs4.graphs.directed_dfs
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.directed\_edge module
--------------------------------------
.. automodule:: itu.algs4.graphs.directed_edge
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.edge module
----------------------------
.. automodule:: itu.algs4.graphs.edge
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.edge\_weighted\_digraph module
-----------------------------------------------
.. automodule:: itu.algs4.graphs.edge_weighted_digraph
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.edge\_weighted\_directed\_cycle module
-------------------------------------------------------
.. automodule:: itu.algs4.graphs.edge_weighted_directed_cycle
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.edge\_weighted\_directed\_cycle\_anton module
--------------------------------------------------------------
.. automodule:: itu.algs4.graphs.edge_weighted_directed_cycle_anton
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.edge\_weighted\_graph module
---------------------------------------------
.. automodule:: itu.algs4.graphs.edge_weighted_graph
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.graph module
-----------------------------
.. automodule:: itu.algs4.graphs.graph
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.kosaraju\_sharir\_scc module
---------------------------------------------
.. automodule:: itu.algs4.graphs.kosaraju_sharir_scc
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.kruskal\_mst module
------------------------------------
.. automodule:: itu.algs4.graphs.kruskal_mst
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.lazy\_prim\_mst module
---------------------------------------
.. automodule:: itu.algs4.graphs.lazy_prim_mst
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.prim\_mst module
---------------------------------
.. automodule:: itu.algs4.graphs.prim_mst
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.symbol\_digraph module
---------------------------------------
.. automodule:: itu.algs4.graphs.symbol_digraph
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.symbol\_graph module
-------------------------------------
.. automodule:: itu.algs4.graphs.symbol_graph
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.topological module
-----------------------------------
.. automodule:: itu.algs4.graphs.topological
:members:
:undoc-members:
:show-inheritance:
itu.algs4.graphs.transitive\_closure module
-------------------------------------------
.. automodule:: itu.algs4.graphs.transitive_closure
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: itu.algs4.graphs
:members:
:undoc-members:
:show-inheritance:
================================================
FILE: docs/source/itu.algs4.rst
================================================
itu.algs4 package
=================
Subpackages
-----------
.. toctree::
:maxdepth: 4
itu.algs4.errors
itu.algs4.fundamentals
itu.algs4.graphs
itu.algs4.searching
itu.algs4.sorting
itu.algs4.stdlib
itu.algs4.strings
Module contents
---------------
.. automodule:: itu.algs4
:members:
:undoc-members:
:show-inheritance:
================================================
FILE: docs/source/itu.algs4.searching.rst
================================================
itu.algs4.searching package
===========================
Submodules
----------
itu.algs4.searching.binary\_search\_st module
---------------------------------------------
.. automodule:: itu.algs4.searching.binary_search_st
:members:
:undoc-members:
:show-inheritance:
itu.algs4.searching.bst module
------------------------------
.. automodule:: itu.algs4.searching.bst
:members:
:undoc-members:
:show-inheritance:
itu.algs4.searching.file\_index module
--------------------------------------
.. automodule:: itu.algs4.searching.file_index
:members:
:undoc-members:
:show-inheritance:
itu.algs4.searching.frequency\_counter module
---------------------------------------------
.. automodule:: itu.algs4.searching.frequency_counter
:members:
:undoc-members:
:show-inheritance:
itu.algs4.searching.linear\_probing\_hst module
-----------------------------------------------
.. automodule:: itu.algs4.searching.linear_probing_hst
:members:
:undoc-members:
:show-inheritance:
itu.algs4.searching.lookup\_csv module
--------------------------------------
.. automodule:: itu.algs4.searching.lookup_csv
:members:
:undoc-members:
:show-inheritance:
itu.algs4.searching.lookup\_index module
----------------------------------------
.. automodule:: itu.algs4.searching.lookup_index
:members:
:undoc-members:
:show-inheritance:
itu.algs4.searching.red\_black\_bst module
------------------------------------------
.. automodule:: itu.algs4.searching.red_black_bst
:members:
:undoc-members:
:show-inheritance:
itu.algs4.searching.seperate\_chaining\_hst module
--------------------------------------------------
.. automodule:: itu.algs4.searching.seperate_chaining_hst
:members:
:undoc-members:
:show-inheritance:
itu.algs4.searching.sequential\_search\_st module
-------------------------------------------------
.. automodule:: itu.algs4.searching.sequential_search_st
:members:
:undoc-members:
:show-inheritance:
itu.algs4.searching.set module
------------------------------
.. automodule:: itu.algs4.searching.set
:members:
:undoc-members:
:show-inheritance:
itu.algs4.searching.sparse\_vector module
-----------------------------------------
.. automodule:: itu.algs4.searching.sparse_vector
:members:
:undoc-members:
:show-inheritance:
itu.algs4.searching.st module
-----------------------------
.. automodule:: itu.algs4.searching.st
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: itu.algs4.searching
:members:
:undoc-members:
:show-inheritance:
================================================
FILE: docs/source/itu.algs4.sorting.rst
================================================
itu.algs4.sorting package
=========================
Submodules
----------
itu.algs4.sorting.heap module
-----------------------------
.. automodule:: itu.algs4.sorting.heap
:members:
:undoc-members:
:show-inheritance:
itu.algs4.sorting.index\_min\_pq module
---------------------------------------
.. automodule:: itu.algs4.sorting.index_min_pq
:members:
:undoc-members:
:show-inheritance:
itu.algs4.sorting.insertion\_sort module
----------------------------------------
.. automodule:: itu.algs4.sorting.insertion_sort
:members:
:undoc-members:
:show-inheritance:
itu.algs4.sorting.max\_pq module
--------------------------------
.. automodule:: itu.algs4.sorting.max_pq
:members:
:undoc-members:
:show-inheritance:
itu.algs4.sorting.merge module
------------------------------
.. automodule:: itu.algs4.sorting.merge
:members:
:undoc-members:
:show-inheritance:
itu.algs4.sorting.merge\_bu module
----------------------------------
.. automodule:: itu.algs4.sorting.merge_bu
:members:
:undoc-members:
:show-inheritance:
itu.algs4.sorting.min\_pq module
--------------------------------
.. automodule:: itu.algs4.sorting.min_pq
:members:
:undoc-members:
:show-inheritance:
itu.algs4.sorting.quick3way module
----------------------------------
.. automodule:: itu.algs4.sorting.quick3way
:members:
:undoc-members:
:show-inheritance:
itu.algs4.sorting.quicksort module
----------------------------------
.. automodule:: itu.algs4.sorting.quicksort
:members:
:undoc-members:
:show-inheritance:
itu.algs4.sorting.selection module
----------------------------------
.. automodule:: itu.algs4.sorting.selection
:members:
:undoc-members:
:show-inheritance:
itu.algs4.sorting.shellsort module
----------------------------------
.. automodule:: itu.algs4.sorting.shellsort
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: itu.algs4.sorting
:members:
:undoc-members:
:show-inheritance:
================================================
FILE: docs/source/itu.algs4.stdlib.rst
================================================
itu.algs4.stdlib package
========================
Submodules
----------
itu.algs4.stdlib.binary\_out module
-----------------------------------
.. automodule:: itu.algs4.stdlib.binary_out
:members:
:undoc-members:
:show-inheritance:
itu.algs4.stdlib.binary\_stdin module
-------------------------------------
.. automodule:: itu.algs4.stdlib.binary_stdin
:members:
:undoc-members:
:show-inheritance:
itu.algs4.stdlib.binary\_stdout module
--------------------------------------
.. automodule:: itu.algs4.stdlib.binary_stdout
:members:
:undoc-members:
:show-inheritance:
itu.algs4.stdlib.color module
-----------------------------
.. automodule:: itu.algs4.stdlib.color
:members:
:undoc-members:
:show-inheritance:
itu.algs4.stdlib.instream module
--------------------------------
.. automodule:: itu.algs4.stdlib.instream
:members:
:undoc-members:
:show-inheritance:
itu.algs4.stdlib.outstream module
---------------------------------
.. automodule:: itu.algs4.stdlib.outstream
:members:
:undoc-members:
:show-inheritance:
itu.algs4.stdlib.picture module
-------------------------------
.. automodule:: itu.algs4.stdlib.picture
:members:
:undoc-members:
:show-inheritance:
itu.algs4.stdlib.stdarray module
--------------------------------
.. automodule:: itu.algs4.stdlib.stdarray
:members:
:undoc-members:
:show-inheritance:
itu.algs4.stdlib.stdaudio module
--------------------------------
.. automodule:: itu.algs4.stdlib.stdaudio
:members:
:undoc-members:
:show-inheritance:
itu.algs4.stdlib.stddraw module
-------------------------------
.. automodule:: itu.algs4.stdlib.stddraw
:members:
:undoc-members:
:show-inheritance:
itu.algs4.stdlib.stdio module
-----------------------------
.. automodule:: itu.algs4.stdlib.stdio
:members:
:undoc-members:
:show-inheritance:
itu.algs4.stdlib.stdrandom module
---------------------------------
.. automodule:: itu.algs4.stdlib.stdrandom
:members:
:undoc-members:
:show-inheritance:
itu.algs4.stdlib.stdstats module
--------------------------------
.. automodule:: itu.algs4.stdlib.stdstats
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: itu.algs4.stdlib
:members:
:undoc-members:
:show-inheritance:
================================================
FILE: docs/source/itu.algs4.strings.rst
================================================
itu.algs4.strings package
=========================
Submodules
----------
itu.algs4.strings.boyer\_moore module
-------------------------------------
.. automodule:: itu.algs4.strings.boyer_moore
:members:
:undoc-members:
:show-inheritance:
itu.algs4.strings.huffman\_compression module
---------------------------------------------
.. automodule:: itu.algs4.strings.huffman_compression
:members:
:undoc-members:
:show-inheritance:
itu.algs4.strings.kmp module
----------------------------
.. automodule:: itu.algs4.strings.kmp
:members:
:undoc-members:
:show-inheritance:
itu.algs4.strings.lsd module
----------------------------
.. automodule:: itu.algs4.strings.lsd
:members:
:undoc-members:
:show-inheritance:
itu.algs4.strings.lzw module
----------------------------
.. automodule:: itu.algs4.strings.lzw
:members:
:undoc-members:
:show-inheritance:
itu.algs4.strings.msd module
----------------------------
.. automodule:: itu.algs4.strings.msd
:members:
:undoc-members:
:show-inheritance:
itu.algs4.strings.nfa module
----------------------------
.. automodule:: itu.algs4.strings.nfa
:members:
:undoc-members:
:show-inheritance:
itu.algs4.strings.quick3string module
-------------------------------------
.. automodule:: itu.algs4.strings.quick3string
:members:
:undoc-members:
:show-inheritance:
itu.algs4.strings.rabin\_karp module
------------------------------------
.. automodule:: itu.algs4.strings.rabin_karp
:members:
:undoc-members:
:show-inheritance:
itu.algs4.strings.trie\_st module
---------------------------------
.. automodule:: itu.algs4.strings.trie_st
:members:
:undoc-members:
:show-inheritance:
itu.algs4.strings.tst module
----------------------------
.. automodule:: itu.algs4.strings.tst
:members:
:undoc-members:
:show-inheritance:
Module contents
---------------
.. automodule:: itu.algs4.strings
:members:
:undoc-members:
:show-inheritance:
================================================
FILE: examples/bst.py
================================================
#!/usr/bin/env python3
import sys
from itu.algs4.searching.bst import BST
from itu.algs4.stdlib import stdio
if __name__ == "__main__":
if len(sys.argv) > 1:
try:
sys.stdin = open(sys.argv[1])
except IOError:
print("File not found, using standard input instead")
data = stdio.readAllStrings()
st: BST[str, int] = BST()
i = 0
for key in data:
st.put(key, i)
i += 1
print("LEVELORDER:")
for key in st.level_order():
print(str(key) + " " + str(st.get(key)))
print()
print("KEYS:")
for key in st.keys():
print(str(key) + " " + str(st.get(key)))
================================================
FILE: examples/hello_world.py
================================================
#!/usr/bin/env python3
from itu.algs4.stdlib import stdio
stdio.write("Hello World!\n")
================================================
FILE: examples/queue.py
================================================
#!/usr/bin/env python3
from itu.algs4.fundamentals.queue import Queue
from itu.algs4.stdlib import stdio
"""
Reads strings from an stdin and adds them to a queue.
When reading a '-' it removes the least recently added item and prints it.
Prints the amount of items left on the queue.
"""
queue: Queue[str] = Queue()
while not stdio.isEmpty():
input_item = stdio.readString()
if input_item != "-":
queue.enqueue(input_item)
elif not queue.is_empty():
print(queue.dequeue())
print("({} left on queue)".format(queue.size()))
================================================
FILE: examples/sort-numbers.py
================================================
#!/usr/bin/env python3
from itu.algs4.sorting import merge
from itu.algs4.stdlib import stdio
"""
Reads a list of integers from standard input.
Then prints it in sorted order.
"""
L = stdio.readAllInts()
merge.sort(L)
if len(L) > 0:
stdio.write(L[0])
for i in range(1, len(L)):
stdio.write(" ")
stdio.write(L[i])
stdio.writeln()
================================================
FILE: examples/stack.py
================================================
#!/usr/bin/env python3
import sys
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.stdlib import stdio
if len(sys.argv) > 1:
try:
sys.stdin = open(sys.argv[1])
except IOError:
print("File not found, using standard input instead")
stack: Stack[str] = Stack()
while not stdio.isEmpty():
item = stdio.readString()
if not item == "-":
stack.push(item)
elif not stack.is_empty():
stdio.write(stack.pop() + " ")
stdio.writef("(%i left on stack)\n", stack.size())
================================================
FILE: itu/__init__.py
================================================
================================================
FILE: itu/algs4/__init__.py
================================================
================================================
FILE: itu/algs4/errors/__init__.py
================================================
================================================
FILE: itu/algs4/errors/errors.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
class NoSuchElementException(Exception):
pass
class IllegalArgumentException(Exception):
pass
class UnsupportedOperationException(Exception):
pass
================================================
FILE: itu/algs4/fundamentals/__init__.py
================================================
================================================
FILE: itu/algs4/fundamentals/bag.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
# See ResizingArrayBag for a version that uses a resizing array.
from typing import Generic, Iterator, Optional, TypeVar
T = TypeVar("T")
S = TypeVar("S")
class Bag(Generic[T]):
"""The Bag class represents a bag (or multiset) of generic items. It
supports insertion and iterating over the items in arbitrary order.
This implementation uses a singly linked list with a static nested class Node.
See LinkedBag for the version from the
textbook that uses a non-static nested class.
The add, is_empty, and size operations
take constant time. Iteration takes time proportional to the number of items.
"""
class Node(Generic[S]):
# helper linked list class
def __init__(self):
self.next: Optional[Bag.Node[T]] = None
self.item: Optional[S] = None
def __init__(self) -> None:
"""Initializes an empty bag."""
self._first: Optional[Bag.Node[T]] = None # beginning of bag
self._n = 0 # number of elements in bag
def is_empty(self) -> bool:
"""Returns true if this bag is empty.
:returns: true if this bag is empty
false otherwise
"""
return self._first is None
def size(self) -> int:
"""Returns the number of items in this bag.
:returns: the number of items in this bag
"""
return self._n
def __len__(self) -> int:
return self.size()
def add(self, item: T) -> None:
"""Adds the item to this bag.
:param item: the item to add to this bag
"""
oldfirst = self._first
self._first = Bag.Node()
self._first.item = item
self._first.next = oldfirst
self._n += 1
def __iter__(self) -> Iterator[T]:
"""Returns an iterator that iterates over the items in this bag in
arbitrary order.
:returns: an iterator that iterates over the items in this bag in arbitrary order
"""
current = self._first
while current is not None:
assert current.item is not None
yield current.item
current = current.next
def __repr__(self) -> str:
out = "{"
for elem in self:
out += "{}, ".format(elem)
return out + "}"
# start of the script itself
if __name__ == "__main__":
import sys
from itu.algs4.stdlib import stdio
if len(sys.argv) > 1:
try:
sys.stdin = open(sys.argv[1])
except IOError:
print("File not found, using standard input instead")
bag: Bag[str] = Bag()
while not stdio.isEmpty():
item = stdio.readString()
bag.add(item)
stdio.writef("size of bag = %i\n", bag.size())
for s in bag:
stdio.writeln(s)
================================================
FILE: itu/algs4/fundamentals/binary_search.py
================================================
import sys
from typing import List, TypeVar
from itu.algs4.stdlib import stdio
T = TypeVar("T")
# Created for BADS 2018
# See README.md for details
# This is python3
"""
The binary_search module provides a method for binary
searching for an item in a sorted array.
The index_of operation takes logarithmic time in the worst case.
"""
def index_of(a: List[T], key: T):
"""Returns the index of the specified key in the specified array.
:param a: the array of items, must be sorted in ascending order
:param key: the search key
:return: index of key in array if present -1 otherwise
"""
lo = 0
hi = len(a) - 1
while hi >= lo:
mid = lo + (hi - lo) // 2
if a[mid] < key:
lo = mid + 1
elif a[mid] > key:
hi = mid - 1
else:
return mid
return -1
def main():
"""Reads strings from first input file and sorts them Reads strings from
second input file and prints every string not in first input file."""
if len(sys.argv) == 3:
sys.stdin = open(sys.argv[1])
arr = stdio.readAllStrings()
arr.sort()
sys.stdin = open(sys.argv[2])
while not stdio.isEmpty():
key = stdio.readString()
if index_of(arr, key) == -1:
print(key)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/fundamentals/evaluate.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
import math
import sys
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.stdlib import stdio
def evaluate():
ops = Stack()
vals = Stack()
while not stdio.isEmpty():
# Read token, push if operator
s = stdio.readString()
if s == "(":
pass
elif s == "+":
ops.push(s)
elif s == "-":
ops.push(s)
elif s == "*":
ops.push(s)
elif s == "/":
ops.push(s)
elif s == "sqrt":
ops.push(s)
elif s == ")":
# Pop, evaluate and push result if token is ")"
op = ops.pop()
v = vals.pop()
if op == "+":
v = vals.pop() + v
elif op == "-":
v = vals.pop() - v
elif op == "*":
v = vals.pop() * v
elif op == "/":
v = vals.pop() / v
elif op == "sqrt":
v = math.sqrt(v)
vals.push(v)
else:
vals.push(float(s))
stdio.writeln(vals.pop())
if __name__ == "__main__":
if len(sys.argv) > 1:
try:
sys.stdin = open(sys.argv[1])
except IOError:
print("File not found, using standard input instead")
evaluate()
================================================
FILE: itu/algs4/fundamentals/java_helper.py
================================================
# Created for BADS 2018
# See README.md for details
# this one is not inspired by algorithms from the book, but useful for running java and python in parallel
# Python 3
def java_string_hash(key):
"""If key is a string, compute its java .hash() code.
Taken from http://garage.pimentech.net/libcommonPython_src_python_libcommon_javastringhashcode/
"""
h = 0
for c in key:
h = (31 * h + ord(c)) & 0xFFFFFFFF
return ((h + 0x80000000) & 0xFFFFFFFF) - 0x80000000
def trailing_zeros(i):
zeros = 0
while i & 1 == 0 and zeros < 32:
zeros += 1
i = i >> 1
return zeros
================================================
FILE: itu/algs4/fundamentals/queue.py
================================================
from typing import Generic, Iterator, Optional, TypeVar
from ..errors.errors import NoSuchElementException
# Created for BADS 2018
# See README.md for details
# This is python3
T = TypeVar("T")
class Node(Generic[T]):
def __init__(self, item: T, next: Optional["Node[T]"]) -> None:
"""Initializes a new node.
:param item: the item to be stored in the node
:param next: the next node in the queue
"""
self.item: T = item
self.next: Optional[Node[T]] = next
class Queue(Generic[T]):
"""The Queue class represents a first-in-first-out (FIFO) queue of generic
items.
It supports the usual enqueue and dequeue operations, along with
methods for peeking at the first item, testing if the queue is
empty, and iterating through the items in FIFO order This
implementation uses a singly linked list of linked-list nodes The
enqueue, dequeue, peek, size, and is_empty operations all take
constant time in the worst case
"""
def __init__(self) -> None:
"""Initializes an empty queue."""
self._first: Optional[Node[T]] = None
self._last: Optional[Node[T]] = None
self._n: int = 0
def enqueue(self, item: T) -> None:
"""Adds the item to this queue.
:param item: the item to add
"""
old_last: Optional[Node[T]] = self._last
self._last = Node(item, None)
if self.is_empty():
self._first = self._last
else:
assert old_last is not None
old_last.next = self._last
self._n += 1
def dequeue(self) -> T:
"""
Removes and returns the item on this queue that was least recently added.
:return: the item on this queue that was least recently added.
:raises NoSuchElementException: if this queue is empty
"""
if self.is_empty():
raise NoSuchElementException("Queue underflow")
assert self._first is not None
item = self._first.item
self._first = self._first.next
self._n -= 1
if self.is_empty():
self._last = None
return item
def is_empty(self) -> bool:
"""Returns true if this queue is empty.
:return: True if this queue is empty otherwise False
:rtype: bool
"""
return self._first is None
def size(self) -> int:
"""Returns the number of items in this queue.
:return: the number of items in this queue
:rtype: int
"""
return self._n
def __len__(self) -> int:
return self.size()
def peek(self) -> T:
"""
Returns the item least recently added to this queue.
:return: the item least recently added to this queue
:raises NoSuchElementException: if this queue is empty
"""
if self.is_empty():
raise NoSuchElementException("Queue underflow")
assert self._first is not None
return self._first.item
def __iter__(self) -> Iterator[T]:
"""Iterates over all the items in this queue in FIFO order."""
curr = self._first
while curr is not None:
yield curr.item
curr = curr.next
def __repr__(self) -> str:
"""Returns a string representation of this queue.
:return: the sequence of items in FIFO order, separated by spaces
"""
s = []
for item in self:
s.append("{} ".format(item))
return "".join(s)
================================================
FILE: itu/algs4/fundamentals/stack.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
from typing import Generic, Iterator, List, Optional, TypeVar
T = TypeVar("T")
class Node(Generic[T]):
# helper linked list class
def __init__(self):
self.item: T = None
self.next: Optional[Node] = None
class Stack(Generic[T]):
"""The Stack class represents a last-in-first-out (LIFO) stack of generic
items. It supports the usual push and pop operations, along with methods
for peeking at the top item, testing if the stack is empty, and iterating
through the items in LIFO order.
This implementation uses a singly linked list with a static nested
class for linked-list nodes. See LinkedStack for the version from
the textbook that uses a non-static nested class. See
ResizingArrayStack for a version that uses a resizing array. The
push, pop, peek, size, and is-empty operations all take constant
time in the worst case.
"""
def __init__(self) -> None:
"""Initializes an empty stack."""
self._first: Optional[Node[T]] = None
self._n: int = 0
def is_empty(self) -> bool:
"""Returns true if this stack is empty.
:returns: true if this stack is empty false otherwise
"""
return self._n == 0
def size(self) -> int:
"""Returns the number of items in this stack.
:returns: the number of items in this stack
"""
return self._n
def __len__(self) -> int:
return self.size()
def push(self, item: T) -> None:
"""Adds the item to this stack.
:param item: the item to add
"""
oldfirst = self._first
self._first = Node()
self._first.item = item
self._first.next = oldfirst
self._n += 1
def pop(self) -> T:
"""Removes and returns the item most recently added to this stack.
:returns: the item most recently added
:raises ValueError: if this stack is empty
"""
if self.is_empty():
raise ValueError("Stack underflow")
assert self._first is not None
item = self._first.item
assert item is not None
self._first = self._first.next
self._n -= 1
return item
def peek(self) -> T:
"""Returns (but does not remove) the item most recently added to this
stack.
:returns: the item most recently added to this stack
:raises ValueError: if this stack is empty
"""
if self.is_empty():
raise ValueError("Stack underflow")
assert self._first is not None
item = self._first.item
assert item is not None
return item
def __repr__(self) -> str:
"""Returns a string representation of this stack.
:returns: the sequence of items in this stack in LIFO order, separated by spaces
"""
s = []
for item in self:
s.append(item.__repr__())
return " ".join(s)
def __iter__(self) -> Iterator[T]:
"""Returns an iterator to this stack that iterates through the items in
LIFO order.
:return: an iterator to this stack that iterates through the items in LIFO order
"""
current = self._first
while current is not None:
item = current.item
assert item is not None
yield item
current = current.next
class FixedCapacityStack(Generic[T]):
def __init__(self, capacity: int):
self.a: List[Optional[T]] = [None] * capacity
self.n: int = 0
def is_empty(self) -> bool:
return self.n == 0
def size(self) -> int:
return self.n
def __len__(self) -> int:
return self.size()
def push(self, item: T):
self.a[self.n] = item
self.n += 1
def pop(self) -> T:
self.n -= 1
item = self.a[self.n]
assert item is not None
return item
class ResizingArrayStack(Generic[T]):
def __init__(self) -> None:
self.a: List[Optional[T]] = [None]
self.n: int = 0
def is_empty(self) -> bool:
return self.n == 0
def size(self) -> int:
return self.n
def __len__(self) -> int:
return self.size()
def resize(self, capacity: int) -> None:
temp: List[Optional[T]] = [None] * capacity
for i in range(self.n):
temp[i] = self.a[i]
self.a = temp
def push(self, item: T) -> None:
if self.n == len(self.a):
self.resize(2 * len(self.a))
self.a[self.n] = item
self.n += 1
def pop(self) -> T:
self.n -= 1
item = self.a[self.n]
self.a[self.n] = None
if self.n > 0 and self.n <= len(self.a) // 4:
self.resize(len(self.a) // 2)
assert item is not None
return item
def __iter__(self) -> Iterator[T]:
i = self.n - 1
while i >= 0:
item = self.a[i]
assert item is not None
yield item
i -= 1
================================================
FILE: itu/algs4/fundamentals/three_sum.py
================================================
class ThreeSum:
@staticmethod
def count(a):
# Count triples that sum to 0
n = len(a)
count = 0
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
if a[i] + a[j] + a[k] == 0:
count += 1
return count
================================================
FILE: itu/algs4/fundamentals/three_sum_fast.py
================================================
from itu.algs4.fundamentals import binary_search
class ThreeSumFast:
@staticmethod
def count(a):
# Count triples that sum to 0
a.sort()
n = len(a)
count = 0
for i in range(n):
for j in range(i + 1, n):
if binary_search.index_of(a, -a[i] - a[j]) > j:
count += 1
return count
================================================
FILE: itu/algs4/fundamentals/two_sum_fast.py
================================================
from itu.algs4.fundamentals import binary_search
class TwoSumFast:
@staticmethod
def count(a):
# Count pairs that sum to 0
a = sorted(a)
n = len(a)
count = 0
for i in range(n):
if binary_search.index_of(a, -a[i]) > i:
count += 1
return count
================================================
FILE: itu/algs4/fundamentals/uf.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
"""The UF module implements several versions of the union-find data structure
(also known as the disjoint-sets data type). It supports the union and find
operations, along with a connected operation for determining whether two sites
are in the same component and a count operation that returns the total number
of components.
The union-find data type models connectivity among a set of n sites, named 0
through n-1. The is-connected-to relation must be an equivalence relation:
* Reflexive: p is connected to p.
* Symmetric: If p is connected to q, then q is connected to p.
* Transitive: If p is connected to q and q is connected to r, then
p is connected to r.
"""
import sys
from itu.algs4.stdlib import stdio
class UF:
"""
This is an implementation of the union-find data structure - see module documentation for
more info.
This implementation uses weighted quick union by rank with path compression by
halving. Initializing a data structure with n sites takes linear time. Afterwards,
the union, find, and connected operations take logarithmic time (in the worst case)
and the count operation takes constant time. Moreover, the amortized time per union,
find, and connected operation has inverse Ackermann complexity.
For additional documentation, see Section 1.5 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
"""
def __init__(self, n: int) -> None:
"""Initializes an empty union-find data structure with n sites, 0
through n-1. Each site is initially in its own component.
:param n: the number of sites
"""
self._count = n
self._parent = list(range(n))
self._rank = [0] * n
def _validate(self, p: int) -> None:
# validate that p is a valid index
n = len(self._parent)
if p < 0 or p >= n:
raise ValueError("index {} is not between 0 and {}".format(p, n - 1))
def union(self, p: int, q: int) -> None:
"""Merges the component containing site p with the component containing
site q.
:param p: the integer representing one site
:param q: the integer representing the other site
"""
root_p = self.find(p)
root_q = self.find(q)
if root_p == root_q:
return
# make root of smaller rank point to root of larger rank
if self._rank[root_p] < self._rank[root_q]:
self._parent[root_p] = root_q
elif self._rank[root_p] > self._rank[root_q]:
self._parent[root_q] = root_p
else:
self._parent[root_q] = root_p
self._rank[root_p] += 1
self._count -= 1
def find(self, p: int) -> int:
"""Returns the component identifier for the component containing site
p.
:param p: the integer representing one site
:return: the component identifier for the component containing site p
"""
self._validate(p)
while p != self._parent[p]:
self._parent[p] = self._parent[
self._parent[p]
] # path compression by halving
p = self._parent[p]
return p
def connected(self, p: int, q: int) -> bool:
"""Returns true if the two sites are in the same component.
:param p: the integer representing one site
:param q: the integer representing the other site
:return: true if the two sites p and q are in the same component; false otherwise
"""
return self.find(p) == self.find(q)
def count(self) -> int:
return self._count
class QuickUnionUF:
"""
This is an implementation of the union-find data structure - see module documentation for
more info.
This implementation uses quick union. Initializing a data structure with n sites takes
linear time. Afterwards, the union, find, and connected operations take linear time
(in the worst case) and the count operation takes constant time. For alternate implementations
of the same API, see UF, QuickFindUF, and WeightedQuickUnionUF.
For additional documentation, see Section 1.5 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
"""
def __init__(self, n: int) -> None:
"""Initializes an empty union-find data structure with n sites, 0
through n-1. Each site is initially in its own component.
:param n: the number of sites
"""
self._count = n
self._parent = list(range(n))
def _validate(self, p: int) -> None:
# validate that p is a valid index
n = len(self._parent)
if p < 0 or p >= n:
raise ValueError("index {} is not between 0 and {}".format(p, n - 1))
def union(self, p: int, q: int) -> None:
"""Merges the component containing site p with the component containing
site q.
:param p: the integer representing one site
:param q: the integer representing the other site
"""
root_p = self.find(p)
root_q = self.find(q)
if root_p == root_q:
return
self._parent[root_p] = root_q
self._count -= 1
def find(self, p: int) -> int:
"""Returns the component identifier for the component containing site
p.
:param p: the integer representing one site
:return: the component identifier for the component containing site p
"""
self._validate(p)
while p != self._parent[p]:
p = self._parent[p]
return p
def connected(self, p: int, q: int) -> bool:
"""Returns true if the two sites are in the same component.
:param p: the integer representing one site
:param q: the integer representing the other site
:return: true if the two sites p and q are in the same component; false otherwise
"""
return self.find(p) == self.find(q)
def count(self) -> int:
return self._count
class WeightedQuickUnionUF:
"""
This is an implementation of the union-find data structure - see module documentation for
more info.
This implementation uses weighted quick union by size (without path compression).
Initializing a data structure with n sites takes linear time. Afterwards, the union, find,
and connected operations take logarithmic time (in the worst case) and the count operation
takes constant time. For alternate implementations of the same API, see UF, QuickFindUF,
and QuickUnionUF.
For additional documentation, see Section 1.5 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
"""
def __init__(self, n: int) -> None:
"""Initializes an empty union-find data structure with n sites, 0
through n-1. Each site is initially in its own component.
:param n: the number of sites
"""
self._count = n
self._parent = list(range(n))
self._size = [1] * n
def _validate(self, p: int) -> None:
# validate that p is a valid index
n = len(self._parent)
if p < 0 or p >= n:
raise ValueError("index {} is not between 0 and {}".format(p, n - 1))
def union(self, p: int, q: int) -> None:
"""Merges the component containing site p with the component containing
site q.
:param p: the integer representing one site
:param q: the integer representing the other site
"""
root_p = self.find(p)
root_q = self.find(q)
if root_p == root_q:
return
# make root of smaller rank point to root of larger rank
if self._size[root_p] < self._size[root_q]:
small, large = root_p, root_q
else:
small, large = root_q, root_p
self._parent[small] = large
self._size[large] += self._size[small]
self._count -= 1
def find(self, p: int) -> int:
"""Returns the component identifier for the component containing site
p.
:param p: the integer representing one site
:return: the component identifier for the component containing site p
"""
self._validate(p)
while p != self._parent[p]:
p = self._parent[p]
return p
def connected(self, p: int, q: int) -> bool:
"""Returns true if the two sites are in the same component.
:param p: the integer representing one site
:param q: the integer representing the other site
:return: true if the two sites p and q are in the same component; false otherwise
"""
return self.find(p) == self.find(q)
def count(self) -> int:
return self._count
class QuickFindUF:
"""
This is an implementation of the union-find data structure - see module documentation for
more info.
This implementation uses quick find. Initializing a data structure with n sites takes linear time.
Afterwards, the find, connected, and count operations take constant time but the union operation
takes linear time.
For additional documentation, see Section 1.5 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
"""
def __init__(self, n: int) -> None:
"""Initializes an empty union-find data structure with n sites, 0
through n-1. Each site is initially in its own component.
:param n: the number of sites
"""
self._count = n
self._id = list(range(n))
def _validate(self, p: int) -> None:
# validate that p is a valid index
n = len(self._id)
if p < 0 or p >= n:
raise ValueError("index {} is not between 0 and {}".format(p, n - 1))
def union(self, p: int, q: int) -> None:
"""Merges the component containing site p with the component containing
site q.
:param p: the integer representing one site
:param q: the integer representing the other site
"""
self._validate(p)
self._validate(q)
p_id = self._id[p] # needed for correctness
q_id = self._id[q] # to reduce the number of array accesses
# p and q are already in the same component
if p_id == q_id:
return
for i in range(len(self._id)):
if self._id[i] == p_id:
self._id[i] = q_id
self._count -= 1
def find(self, p: int) -> int:
"""Returns the component identifier for the component containing site
p.
:param p: the integer representing one site
:return: the component identifier for the component containing site p
"""
self._validate(p)
return self._id[p]
def connected(self, p: int, q: int) -> bool:
"""Returns true if the two sites are in the same component.
:param p: the integer representing one site
:param q: the integer representing the other site
:return: true if the two sites p and q are in the same component; false otherwise
"""
self._validate(p)
self._validate(q)
return self._id[p] == self._id[q]
def count(self):
return self._count
# Reads in a an integer n and a sequence of pairs of integers
# (between 0 and n-1) from standard input or a file
# supplied as argument to the program, where each integer
# in the pair represents some site; if the sites are in different
# components, merge the two components and print the pair to standard output.
if __name__ == "__main__":
if len(sys.argv) > 1:
try:
sys.stdin = open(sys.argv[1])
except IOError:
print("File not found, using standard input instead")
n = stdio.readInt()
uf = UF(n)
while not stdio.isEmpty():
p = stdio.readInt()
q = stdio.readInt()
if uf.connected(p, q):
continue
uf.union(p, q)
print("{} {}".format(p, q))
print("number of components: {}".format(uf.count()))
================================================
FILE: itu/algs4/graphs/Arbitrage.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
import math
import sys
from itu.algs4.graphs.bellman_ford_sp import BellmanFordSP
from itu.algs4.graphs.directed_edge import DirectedEdge
from itu.algs4.graphs.edge_weighted_digraph import EdgeWeightedDigraph
from itu.algs4.stdlib import stdio
if __name__ == "__main__":
"""The Arbitrage function provides a client that finds an arbitrage
opportunity in a currency exchange table by constructing a complete-digraph
representation of the exchange table and then finding a negative cycle in
the digraph.
This implementation uses the Bellman-Ford algorithm to find a negative cycle in
the complete digraph. The running time is proportional to V3 in the worst case,
where V is the number of currencies.
For additional documentation, see Section 4.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
"""
if len(sys.argv) > 1:
try:
sys.stdin = open(sys.argv[1])
except IOError:
print("File not found, using standard input instead")
# V currencies
V = stdio.readInt()
name = [None] * V
# Create complete network
graph = EdgeWeightedDigraph(V)
for v in range(V):
name[v] = stdio.readString()
for w in range(V):
rate = stdio.readFloat()
edge = DirectedEdge(v, w, -math.log(rate))
graph.add_edge(edge)
# find negative cycle
spt = BellmanFordSP(graph, 0)
if spt.has_negative_cycle():
stake = 1000.0
for edge in spt.negative_cycle():
print("{} {}", stake, name[edge.from_vertex()])
stake *= math.exp(-edge.weight())
print("{} {}")
================================================
FILE: itu/algs4/graphs/CPM.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
import sys
from itu.algs4.graphs.acyclic_lp import AcyclicLp
from itu.algs4.graphs.directed_edge import DirectedEdge
from itu.algs4.graphs.edge_weighted_digraph import EdgeWeightedDigraph
from itu.algs4.stdlib import instream
"""
The cpm module is an example of using graphs to solve the parallel precedence-constrained
job scheduling problem via the critical path method. It reduces the problem to the longest-paths
problem in edge-weighted DAGs. It builds an edge-weighted digraph (which must be a DAG) from the
job-scheduling problem specification, finds the longest-paths tree, and computes the longest-paths
lengths (which are precisely the start times for each job).
This implementation uses AcyclicLP to find a longest path in a DAG. The running time is proportional
to V + E, where V is the number of jobs and E is the number of precedence constraints.
For additional documentation, see Section 4.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
"""
# Try this with the jobsPC.txt data file
if __name__ == "__main__":
# Create stream from file or the standard input,
# depending on whether a file name was passed.
file = sys.argv[1] if len(sys.argv) > 1 else None
stream = instream.InStream(file)
# Number of jobs
N = stream.readInt()
# Source and sink
source, sink = 2 * N, 2 * N + 1
# Construct the network
G = EdgeWeightedDigraph(2 * N + 2)
for i in range(N):
duration = stream.readFloat()
G.add_edge(DirectedEdge(i, i + N, duration))
G.add_edge(DirectedEdge(source, i, 0.0))
G.add_edge(DirectedEdge(i + N, sink, 0.0))
# Precedence constraints
m = stream.readInt()
for _ in range(m):
successor = stream.readInt()
G.add_edge(DirectedEdge(i + N, successor, 0.0))
# Compute longest path
lp = AcyclicLp(G, source)
# Print results
print("Start times:")
for i in range(N):
print("{:4d}: {:5.1f}".format(i, lp.dist_to(i)))
print("Finish time: {:5.1f}".format(lp.dist_to(i)))
================================================
FILE: itu/algs4/graphs/__init__.py
================================================
================================================
FILE: itu/algs4/graphs/acyclic_lp.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
import sys
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.edge_weighted_digraph import EdgeWeightedDigraph
from itu.algs4.graphs.topological import Topological
from itu.algs4.stdlib import instream
"""
This module implements a class for solving the single-source Longest
paths problem in edge-weighted directed acyclic graphs (DAGs), described in
Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
For more information, see chapter 4.2 of the book.
"""
class AcyclicLp:
"""The AcyclicLP class represents a data type for solving the single-source
longest paths problem in edge-weighted directed acyclic graphs (DAGs). The
edge weights can be positive, negative, or zero.
This implementation uses a topological-sort based algorithm. The constructor takes
time proportional to V + E, where V is the number of vertices and E is the number of edges.
Each call to distTo(int) and hasPathTo(int) takes constant time; each call to pathTo(int)
takes time proportional to the number of edges in the shortest path returned.
For additional documentation, see Section 4.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
"""
def __init__(self, edge_weighted_digraph, s):
"""Computes a longest paths tree from s to every other vertex in the
directed acyclic graph G.
:param edge_weighted_digraph: the acyclic digraph
:param s: the source vertex
"""
graph = edge_weighted_digraph
self._dist_to = [float("-inf")] * graph.V()
self._edge_to = [None] * graph.V()
self._validate_vertex(s)
self._dist_to[s] = 0.0
# relax vertices in topological order
topological = Topological(graph)
if not topological.has_order():
print(graph)
raise ValueError("Digraph is not acyclic.")
for v in topological.order():
for edge in graph.adj(v):
self._relax(edge)
def dist_to(self, v):
"""Returns the length of a longest path from the source vertex s to
vertex v.
:param v: the destination vertex
:returns: the length of a longest path from the source vertex s to vertex v;
negative infinity if no such path exists
"""
self._validate_vertex(v)
return self._dist_to[v]
def has_path_to(self, v):
"""Is there a path from the source vertex s to vertex v?"""
return self.dist_to(v) > float("-inf")
def path_to(self, v):
"""Returns a longest path from the source vertex s to vertex v.
:param: the destination vertex
:returns: a longest path from the source vertex s to vertex v as an iterable of edges, and None if no such path
"""
self._validate_vertex(v)
if not self.has_path_to(v):
return None
path = Stack()
edge = self._edge_to[v]
while edge is not None:
path.push(edge)
edge = self._edge_to[edge.from_vertex()]
return path
# relax edge e, but update if you find a *longer* path
def _relax(self, edge):
v, w = edge.from_vertex(), edge.to_vertex()
if self._dist_to[w] < self._dist_to[v] + edge.weight():
self._dist_to[w] = self._dist_to[v] + edge.weight()
self._edge_to[w] = edge
# throw an IllegalArgumentException unless 0 <= v < V
def _validate_vertex(self, v):
V = len(self._dist_to)
if not (0 <= v < V):
raise ValueError("vertex {} is not between 0 and {}".format(v, V - 1))
if __name__ == "__main__":
# Create stream from file or the standard input,
# depending on whether a file name was passed.
stream = sys.argv[1] if len(sys.argv) > 1 else None
d = EdgeWeightedDigraph.from_stream(instream.InStream(stream))
a = AcyclicLp(d, 3)
# print longest paths to all other vertices
for i in range(d.V()):
print("{} to {}".format(i, a.path_to(i)))
================================================
FILE: itu/algs4/graphs/acyclic_sp.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
import math
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.topological import Topological
class AcyclicSP:
"""The AcyclicSP class represents a data type for solving the single-source
shortest paths problem in edge-weighted directed acyclic graphs (DAGs). The
edge weights can be positive, negative, or zero.
This implementation uses a topological-sort based algorithm. The
constructor takes time proportional to V + E, where V is the number
of vertices and E is the number of edges. Each call to distTo(int)
and has_path_to(int) takes constant time each call to pathTo(int)
takes time proportional to the number of edges in the shortest path
returned.
"""
def __init__(self, G, s):
"""Computes a shortest paths tree from s to every other vertex in the
directed acyclic graph G.
:param G: the acyclic digraph
:param s: the source vertex
:raises ValueError: if the digraph is not acyclic
:raises ValueError: unless 0 <= s < V
"""
self._dist_to = [0] * G.V() # _dist_to[v] = distance of shortest s->v path
self._edge_to = [None] * G.V() # _edge_to[v] = last edge on shortest s->v path
self._validate_vertex(s)
for v in range(G.V()):
self._dist_to[v] = math.inf
self._dist_to[s] = 0.0
# visit vertices in toplogical order
topological = Topological(G)
if not topological.has_order():
raise ValueError("Digraph is not acyclic.")
for v in topological.order():
for e in G.adj(v):
self._relax(e)
def _relax(self, e):
v = e.from_vertex()
w = e.to_vertex()
if self._dist_to[w] > self._dist_to[v] + e.weight():
self._dist_to[w] = self._dist_to[v] + e.weight()
self._edge_to[w] = e
def dist_to(self, v):
"""Returns the length of a shortest path from the source vertex s to
vertex v.
:param v: the destination vertex
:returns: the length of a shortest path from the source vertex s to vertex v
math.inf if no such path
:raises ValueError: unless 0 <= v < V
"""
self._validate_vertex(v)
return self._dist_to[v]
def has_path_to(self, v):
"""Is there a path from the source vertex s to vertex v?
:param v: the destination vertex
:returns: true if there is a path from the source vertex
s to vertex v, and false otherwise
:raises ValueError: unless 0 <= v < V
"""
self._validate_vertex(v)
return self._dist_to[v] < math.inf
def path_to(self, v):
"""Returns a shortest path from the source vertex s to vertex v.
:param v: the destination vertex
:returns: a shortest path from the source vertex s to vertex v
as an iterable of edges, and None if no such path
:raises ValueError: unless 0 <= v < V
"""
self._validate_vertex(v)
if not self.has_path_to(v):
return None
path = Stack()
e = self._edge_to[v]
while e is not None:
path.push(e)
e = self._edge_to[e.from_vertex()]
return path
def _validate_vertex(self, v):
# raise an ValueError unless 0 <= v < V
V = len(self._dist_to)
if v < 0 or v >= V:
raise ValueError("vertex {} is not between 0 and {}".format(v, V - 1))
if __name__ == "__main__":
import sys
from itu.algs4.graphs.edge_weighted_digraph import EdgeWeightedDigraph
from itu.algs4.stdlib import stdio
from itu.algs4.stdlib.instream import InStream
In = InStream(sys.argv[1])
s = int(sys.argv[2])
G = EdgeWeightedDigraph.from_stream(In)
# find shortest path from s to each other vertex in DAG
sp = AcyclicSP(G, s)
for v in range(G.V()):
if sp.has_path_to(v):
stdio.writef("%d to %d (%.2f) ", s, v, sp.dist_to(v))
for e in sp.path_to(v):
stdio.writef("%s\t", e.__repr__())
stdio.writeln()
else:
stdio.writef("%d to %d no path\n", s, v)
================================================
FILE: itu/algs4/graphs/bellman_ford_sp.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
import sys
from itu.algs4.errors.errors import (
IllegalArgumentException,
UnsupportedOperationException,
)
from itu.algs4.fundamentals.queue import Queue
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.edge_weighted_digraph import EdgeWeightedDigraph
from itu.algs4.graphs.edge_weighted_directed_cycle import EdgeWeightedDirectedCycle
from itu.algs4.stdlib.instream import InStream
try:
q = Queue()
q.enqueue(1)
except AttributeError:
print("ERROR - Could not import itu.algs4 queue")
sys.exit(1)
# Execution: python BellmanFordSP.py filename.txt s
# Data files: https://algs4.cs.princeton.edu/44sp/tinyEWDn.txt
# https://algs4.cs.princeton.edu/44sp/mediumEWDnc.txt
#
# Bellman-Ford shortest path algorithm. Computes the shortest path tree in
# edge-weighted digraph G from vertex s, or finds a negative cost cycle
# reachable from s.
"""
The BellmanFordSP class represents a data type for solving the
single-source shortest paths problem in edge-weighted digraphs with
no negative cycles.
The edge weights can be positive, negative, or zero.
This class finds either a shortest path from the source vertex s
to every other vertex or a negative cycle reachable from the source vertex.
This implementation uses the Bellman-Ford-Moore algorithm.
The constructor takes time proportional to V (V + E)
in the worst case, where V is the number of vertices and E
is the number of edges.
Each call to distTo(int) and hasPathTo(int),
hasNegativeCycle takes constant time;
each call to pathTo(int) and negativeCycle()
takes time proportional to length of the path returned.
"""
class BellmanFordSP:
# Computes a shortest paths tree from s to every other vertex in
# the edge-weighted digraph G.
# @param G the acyclic digraph
# @param s the source vertex
# @throws IllegalArgumentException unless 0 <= s < V
def __init__(self, G, s):
self._distTo = [
sys.float_info.max
] * G.V() # distTo[v] = distance of shortest s->v path
self._edgeTo = [None] * G.V() # edgeTo[v] = last edge on shortest s->v path
self._onQueue = [False] * G.V() # onQueue[v] = is v currently on the queue?
self._queue = Queue() # queue of vertices to relax
self._cost = 0 # number of calls to relax()
self._cycle = None # negative cycle (or None if no such cycle)
# Bellman-Ford algorithm
self._distTo[s] = 0.0
self._queue.enqueue(s)
self._onQueue[s] = True
while not self._queue.is_empty() and not self.has_negative_cycle():
v = self._queue.dequeue()
self._onQueue[v] = False
self._relax(G, v)
assert self._check(G, s)
# relax vertex v and put other endpoints on queue if changed
def _relax(self, G, v):
for e in G.adj(v):
w = e.to_vertex()
if self._distTo[w] > self._distTo[v] + e.weight():
self._distTo[w] = self._distTo[v] + e.weight()
self._edgeTo[w] = e
if not self._onQueue[w]:
self._queue.enqueue(w)
self._onQueue[w] = True
if self._cost % G.V() == 0:
self._find_negative_cycle()
if self.has_negative_cycle():
return # found a negative cycle
self._cost += 1
# Is there a negative cycle reachable from the source vertex s?
# @return true if there is a negative cycle reachable from the
# source vertex s, and false otherwise
def has_negative_cycle(self):
return self._cycle is not None
# Returns a negative cycle reachable from the source vertex s, or None
# if there is no such cycle.
# @return a negative cycle reachable from the soruce vertex s
# as an iterable of edges, and None if there is no such cycle
def negative_cycle(self):
return self._cycle
# by finding a cycle in predecessor graph
def _find_negative_cycle(self):
V = len(self._edgeTo)
spt = EdgeWeightedDigraph(V)
for v in range(V):
if self._edgeTo[v] is not None:
spt.add_edge(self._edgeTo[v])
finder = EdgeWeightedDirectedCycle(spt)
self._cycle = finder.cycle()
# Returns the length of a shortest path from the source vertex s to vertex v.
# @param v the destination vertex
# @return the length of a shortest path from the source vertex s to vertex v;
# sys.float_info.max if no such path
# @throws UnsupportedOperationException if there is a negative cost cycle reachable
# from the source vertex s
# @throws IllegalArgumentException unless 0 <= v < V
def dist_to(self, v):
self._validate_vertex(v)
if self.has_negative_cycle():
raise UnsupportedOperationException("Negative cost cycle exists")
return self._distTo[v]
# Is there a path from the source s to vertex v?
# @param v the destination vertex
# @return true if there is a path from the source vertex
# s to vertex v, and false otherwise
# @throws IllegalArgumentException unless 0 <= v < V
def has_path_to(self, v):
self._validate_vertex(v)
return self._distTo[v] < sys.float_info.max
# Returns a shortest path from the source s to vertex v.
# @param v the destination vertex
# @return a shortest path from the source s to vertex v
# as an iterable of edges, and None if no such path
# @throws UnsupportedOperationException if there is a negative cost cycle reachable
# from the source vertex s
# @throws IllegalArgumentException unless 0 <= v < V
def path_to(self, v):
self._validate_vertex(v)
if self.has_negative_cycle():
raise UnsupportedOperationException("Negative cost cycle exists")
if not self.has_path_to(v):
return None
path = Stack()
e = self._edgeTo[v]
while e is not None:
path.push(e)
e = self._edgeTo[e.from_vertex()]
return path
# check optimality conditions: either
# (i) there exists a negative cycle reacheable from s
# or
# (ii) for all edges e = v->w: distTo[w] <= distTo[v] + e.weight()
# (ii') for all edges e = v->w on the SPT: distTo[w] == distTo[v] + e.weight()
def _check(self, G, s):
# has a negative cycle
if self.has_negative_cycle():
weight = 0.0
for e in self.negative_cycle():
weight += e.weight()
if weight >= 0.0:
print("error: weight of negative cycle = {}".format(weight))
return False
# no negative cycle reachable from source
else:
# check that distTo[v] and edgeTo[v] are consistent
if self._distTo[s] != 0.0 or self._edgeTo[s] is not None:
print("distanceTo[s] and edgeTo[s] inconsistent")
return False
for v in range(G.V()):
if v == s:
continue
if self._edgeTo[v] is None and self._distTo[v] != sys.float_info.max:
print("distTo[] and edgeTo[] inconsistent")
return False
# check that all edges e = v->w satisfy distTo[w] <= distTo[v] + e.weight()
for v in range(G.V()):
for e in G.adj(v):
w = e.to_vertex()
if self._distTo[v] + e.weight() < self._distTo[w]:
print("edge {} not relaxed".format(e))
return False
# check that all edges e = v->w on SPT satisfy distTo[w] == distTo[v] + e.weight()
for w in range(G.V()):
if self._edgeTo[w] is None:
continue
e = self._edgeTo[w]
v = e.from_vertex()
if w != e.to_vertex():
return False
if self._distTo[v] + e.weight() != self._distTo[w]:
print("edge {} on shortest path not tight".format(e))
return False
print("Satisfies optimality conditions")
print()
return True
# raise an IllegalArgumentException unless 0 <= v < V
def _validate_vertex(self, v):
V = len(self._distTo)
if v < 0 or v >= V:
raise IllegalArgumentException(
"vertex {} is not between 0 and {}".format(v, V - 1)
)
def main(args):
stream = InStream(args[0])
s = int(args[1])
G = EdgeWeightedDigraph.from_stream(stream)
sp = BellmanFordSP(G, s)
# print negative cycle
if sp.has_negative_cycle():
for e in sp.negative_cycle():
print(e)
# print shortest paths
else:
for v in range(G.V()):
if sp.has_path_to(v):
print("{} to {} ({}) ".format(s, v, sp.dist_to(v)))
for e in sp.path_to(v):
print("{}\t".format(e), end="")
print()
else:
print("{} to {} no path".format(s, v))
if __name__ == "__main__":
main(sys.argv[1:])
# * % python BellmanFordSP.py tinyEWDn.txt 0
# * 0 to 0 ( 0.00)
# * 0 to 1 ( 0.93) 0->2 0.26 2->7 0.34 7->3 0.39 3->6 0.52 6->4 -1.25 4->5 0.35 5->1 0.32
# * 0 to 2 ( 0.26) 0->2 0.26
# * 0 to 3 ( 0.99) 0->2 0.26 2->7 0.34 7->3 0.39
# * 0 to 4 ( 0.26) 0->2 0.26 2->7 0.34 7->3 0.39 3->6 0.52 6->4 -1.25
# * 0 to 5 ( 0.61) 0->2 0.26 2->7 0.34 7->3 0.39 3->6 0.52 6->4 -1.25 4->5 0.35
# * 0 to 6 ( 1.51) 0->2 0.26 2->7 0.34 7->3 0.39 3->6 0.52
# * 0 to 7 ( 0.60) 0->2 0.26 2->7 0.34
# *
# * % python BellmanFordSP.py tinyEWDnc.txt 0
# * 4->5 0.35
# * 5->4 -0.66
# *
================================================
FILE: itu/algs4/graphs/bipartite.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.graph import Graph
class Bipartite:
"""The Bipartite class represents a data type for determining whether an
undirected graph is bipartite or whether it has an odd-length cycle. The
isBipartite operation determines whether the graph is bipartite. If so, the
color operation determines a bipartition if not, the oddCycle operation
determines a cycle with an odd number of edges.
This implementation uses depth-first search. The constructor takes
time proportional to V + E (in the worst case), where V is the
number of vertices and E is the number of edges. Afterwards, the
isBipartite and color operations take constant time the oddCycle
operation takes time proportional to the length of the cycle. See
BipartiteX for a nonrecursive version that uses breadth-first
search.
"""
class UnsupportedOperationException(Exception):
pass
def __init__(self, G):
"""Determines whether an undirected graph is bipartite and finds either
a bipartition or an odd-length cycle.
:param G: the graph
"""
self._is_bipartite = True # is the graph bipartite?
self._color = [
False
] * G.V() # color[v] gives vertices on one side of bipartition
self._marked = [False] * G.V() # marked[v] = True if v has been visited in DFS
self._edge_to = [0] * G.V() # edgeTo[v] = last edge on path to v
self._cycle = None # odd-length cycle
for v in range(G.V()):
if not self._marked[v]:
self._dfs(G, v)
assert self._check(G)
def _dfs(self, G, v):
self._marked[v] = True
for w in G.adj(v):
# short circuit if odd-length cycle found
if self._cycle is not None:
return
# found uncolored vertex, so recur
if not self._marked[w]:
self._edge_to[w] = v
self._color[w] = not self._color[v]
self._dfs(G, w)
# if v-w create an odd-length cycle, find it
elif self._color[w] == self._color[v]:
self._is_bipartite = False
self._cycle = Stack()
self._cycle.push(
w
) # don't need this unless you want to include start vertex twice
x = v
while x != w:
self._cycle.push(x)
x = self._edge_to[x]
self._cycle.push(w)
def is_bipartite(self):
"""Returns True if the graph is bipartite.
:returns: True if the graph is bipartite False otherwise
"""
return self._is_bipartite
def color(self, v):
"""Returns the side of the bipartite that vertex v is on.
:param v: the vertex
:returns: the side of the bipartition that vertex v is on two vertices
are in the same side of the bipartition if and only if they have the
same color
:raises IllegalArgumentException: unless 0 <= v < V
:raises UnsupportedOperationException: if this method is called when the graph
is not bipartite
"""
self._validateVertex(v)
if not self._is_bipartite:
raise Bipartite.UnsupportedOperationException("graph is not bipartite")
return self._color[v]
def odd_cycle(self):
"""Returns an odd-length cycle if the graph is not bipartite, and None
otherwise.
:returns: an odd-length cycle if the graph is not bipartite
(and hence has an odd-length cycle), and None otherwise
"""
return self._cycle
def _check(self, G):
# graph is bipartite
if self._is_bipartite:
for v in range(G.V()):
for w in G.adj(v):
if self._color[v] == self._color[w]:
error = "edge {}-{} with {} and {} in same side of bipartition\n".format(
v, w, v, w
)
print(error, file=sys.stderr)
return False
# graph has an odd-length cycle
else:
# verify cycle
first = -1
last = -1
for v in self.odd_cycle():
if first == -1:
first = v
last = v
if first != last:
error = "cycle begins with {} and ends with {}\n".format(first, last)
print(error, file=sys.stderr)
return False
return True
def _validateVertex(self, v):
# raise an ValueError unless 0 <= v < V
V = len(self._marked)
if v < 0 or v >= V:
raise ValueError("vertex {} is not between 0 and {}".format(v, V - 1))
if __name__ == "__main__":
import sys
from itu.algs4.stdlib import stdio
from itu.algs4.stdlib.instream import InStream
In = InStream(sys.argv[1])
G = Graph.from_stream(In)
stdio.writeln(G)
b = Bipartite(G)
if b.is_bipartite():
stdio.writeln("Graph is bipartite")
for v in range(G.V()):
stdio.writef("%i: %i\n", v, b.color(v))
else:
stdio.writeln("Graph has an odd-length cycle: ")
for x in b.odd_cycle():
stdio.writef("%i ", x)
stdio.writeln()
================================================
FILE: itu/algs4/graphs/breadth_first_paths.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
import math
from itu.algs4.fundamentals.queue import Queue
from itu.algs4.fundamentals.stack import Stack
class BreadthFirstPaths:
"""The BreadthFirstPaths class represents a data type for finding shortest
paths (number of edges) from a source vertex s (or a set of source
vertices) to every other vertex in a directed or undirected graph.
This implementation uses breadth-first search. The constructor takes
time proportional to V + E, where V is the number of vertices and E
is the number of edges. Each call to distTo(int) and hasPathTo(int)
takes constant time each call to pathTo(int) takes time proportional
to the length of the path. It uses extra space (not including the
graph) proportional to V.
"""
def __init__(self, G, s):
"""Computes the shortest path between the source vertex s and every
other vertex in the graph G.
:param G: the graph
:param s: the source vertex
:raises ValueError: unless 0 <= s < V
"""
self._marked = [False] * G.V() # Is a shortest path to this vertex known?
self._dist_to = [math.inf] * G.V()
self._edgeTo = [0] * G.V() # last vertex on known path to this vertex
self._validateVertex(s)
self._bfs(G, s)
assert self._check(G, s)
# @staticmethod
# def from_multiple_sources(G, sources):
# """
# Computes the shortest path between any one of the source vertices in sources
# and every other vertex in graph G.
# :param G the graph
# :param sources the source vertices
# :raises ValueError: unless 0 <= s < V for each vertex
# s in sources
# """
# pass
def _bfs(self, G, s):
# breadth-first search from a single source
queue = Queue()
self._dist_to[s] = 0
self._marked[s] = True # Mark the source
queue.enqueue(s) # and put it on the queue.
while not queue.is_empty():
v = queue.dequeue() # Remove next vertex from the queue.
for w in G.adj(v):
if not self._marked[w]:
self._edgeTo[w] = v # For every unmarked adjacent vertex,
self._dist_to[w] = self._dist_to[v] + 1
self._marked[w] = True # mark it because path is known,
queue.enqueue(w) # and add it to the queue.
# def _bfs_multiple_sources(self, G, sources):
# # breadth-first search from multiple sources
# pass
def has_path_to(self, v):
"""Is there a path between the source vertex s (or sources) and vertex
v?
:param v: the vertex
:returns: true if there is a path, and False otherwise
:raises ValueError: unless 0 <= v < V
"""
return self._marked[v]
def dist_to(self, v):
"""Returns the number of edges in a shortest path between the source
vertex s (or sources) and vertex v?
:param v: the vertex
:returns: the number of edges in a shortest path
:raises ValueError: unless 0 <= v < V
"""
self._validateVertex(v)
return self._dist_to[v]
def path_to(self, v):
"""Returns a shortest path between the source vertex s (or sources) and
v, or null if no such path.
:param v: the vertex
:returns: the sequence of vertices on a shortest path, as an Iterable
:raises ValueError: unless 0 <= v < V
"""
if not self.has_path_to(v):
return None
path = Stack()
x = v
while self._dist_to[x] != 0:
path.push(x)
x = self._edgeTo[x]
path.push(x)
return path
def _check(self, G, s):
# check optimality conditions for singe source
# check that the distance of s = 0
if self._dist_to[s] != 0:
stdio.writef("distance of source %i to itself = %i\n", s, self._dist_to[s])
return False
# check that for each edge v-w dist[w] <= dist[v] + 1
# provided v is reachable from s
for v in range(G.V()):
for w in G.adj(v):
# if self.has_path_to(v) != self.has_path_to(w):
# modified for directed graphs
if self.has_path_to(v) and not self.has_path_to(w):
stdio.writef("edge %i-%i\n", v, w)
stdio.writef("has_path_to(%i) = %s\n", v, self.has_path_to(v))
stdio.writef("has_path_to(%i) = %s\n", w, self.has_path_to(w))
return False
if self.has_path_to(v) and (self._dist_to[w] > self._dist_to[v] + 1):
stdio.writef("edge %i-%i\n", v, w)
stdio.writef("dist_to[%i] = %i\n", v, self._dist_to[v])
stdio.writef("dist_to[%i] = %i\n", v, self._dist_to[w])
return False
# check that v = edgeTo[w] satisfies distTo[w] = distTo[v] + 1
# provided v is reachable from s
for w in range(G.V()):
if not self.has_path_to(w) or w == s:
continue
v = self._edgeTo[w]
if self._dist_to[w] != self._dist_to[v] + 1:
stdio.writef("shortest path edge %i-%i\n", v, w)
stdio.writef("dist_to[%i] = %i\n", v, self._dist_to[v])
stdio.writef("dist_to[%i] = %i\n", w, self._dist_to[w])
return False
return True
def _validateVertex(self, v):
# throw an ValueError unless 0 <= v < V
V = len(self._marked)
if v < 0 or v >= V:
raise ValueError("vertex {} is not between 0 and {}".format(v, V - 1))
# def _validateVertices(self, vertices):
# # throw an ValueError unless 0 <= v < V
# pass
class BreadthFirstPathsBook:
def __init__(self, G, s):
self._marked = [False] * G.V() # Is a shortest path to this vertex known?
self._edgeTo = [0] * G.V() # last vertex on known path to this vertex
self._s = s # source
self._bfs(G, s)
def _bfs(self, G, s):
# breadth-first search from a single source
queue = Queue()
self._marked[s] = True # Mark the source
queue.enqueue(s) # and put it on the queue.
while not queue.is_empty():
v = queue.dequeue() # Remove next vertex from the queue.
for w in G.adj(v):
if not self._marked[w]:
self._edgeTo[w] = v # For every unmarked adjacent vertex,
self._marked[w] = True # mark it because path is known,
queue.enqueue(w) # and add it to the queue.
def has_path_to(self, v):
return self._marked[v]
def path_to(self, v):
if not self.has_path_to(v):
return None
path = Stack()
x = v
while x != self._s:
path.push(x)
x = self._edgeTo[x]
path.push(self._s)
return path
if __name__ == "__main__":
import sys
from itu.algs4.graphs.graph import Graph
from itu.algs4.stdlib import stdio
from itu.algs4.stdlib.instream import InStream
In = InStream(sys.argv[1])
G = Graph.from_stream(In)
s = int(sys.argv[2])
bfs = BreadthFirstPaths(G, s)
for v in range(G.V()):
if bfs.has_path_to(v):
stdio.writef("%d to %d (%d): ", s, v, bfs.dist_to(v))
for x in bfs.path_to(v):
if x == s:
stdio.write(x)
else:
stdio.writef("-%i", x)
stdio.writeln()
else:
stdio.writef("%d to %d (-): not connected\n", s, v)
================================================
FILE: itu/algs4/graphs/cc.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
class CC:
"""The CC class represents a data type for determining the connected
components in an undirected graph. The id operation determines in which
connected component a given vertex lies the connected operation determines
whether two vertices are in the same connected component the count
operation determines the number of connected components and the size
operation determines the number of vertices in the connect component
containing a given vertex.
The component identifier of a connected component is one of the
vertices in the connected component: two vertices have the same component
identifier if and only if they are in the same connected component.
This implementation uses depth-first search.
The constructor takes time proportional to V + E
(in the worst case),
where V is the number of vertices and E is the number of edges.
Afterwards, the id, count, connected,
and size operations take constant time.
"""
def __init__(self, G):
"""Computes the connected components of the undirected graph G.
:param G: the undirected graph
"""
self._marked = [False] * G.V() # marked[v] = has vertex v been marked?
self._id = [None] * G.V() # id[v] = id of connected component containing v
self._size = [0] * G.V() # size[id] = number of vertices in given component
self._count = 0 # number of connected components
for v in range(G.V()):
if not self._marked[v]:
self._dfs(G, v)
self._count += 1
def _dfs(self, G, v):
# depth-first search for a Graph
self._marked[v] = True
self._id[v] = self._count
self._size[self._count] += 1
for w in G.adj(v):
if not self._marked[w]:
self._dfs(G, w)
def id(self, v):
"""Returns the component id of the connected component containing
vertex v.
:param v: the vertex
:returns: the component id of the connected component containing vertex v
:raises ValueError: unless 0 <= v < V
"""
self._validate_vertex(v)
return self._id[v]
def size(self, v):
"""Returns the number of vertices in the connected component containing
vertex v.
:param v: the vertex
:returns: the number of vertices in the connected component containing vertex v
:raises ValueError: unless 0 <= v < V
"""
self._validate_vertex(v)
return self._size[self._id[v]]
def count(self):
"""Returns the number of connected components in the graph G.
:returns: the number of connected components in the graph G
"""
return self._count
def connected(self, v, w):
"""Returns true if vertices v and w are in the same connected
component.
:param v: one vertex
:param w: the other vertex
:returns: True if vertices v and w are in the same connected component;
False otherwise
:raises ValueError: unless 0 <= v < V
:raises ValueError: unless 0 <= w < V
"""
self._validate_vertex(v)
self._validate_vertex(w)
return self.id(v) == self.id(w)
def _validate_vertex(self, v):
# Raises a ValueError n unless 0 <= v < V
V = len(self._marked)
if v < 0 or v >= V:
raise ValueError("vertex {} is not between 0 and {}".format(v, V - 1))
class CCBook:
def __init__(self, G):
self._marked = [False] * G.V() # marked[v] = has vertex v been marked?
self._id = [None] * G.V() # id[v] = id of connected component containing v
self._count = 0 # number of connected components
for s in range(G.V()):
if not self._marked[s]:
self._dfs(G, s)
self._count += 1
def _dfs(self, G, v):
self._marked[v] = True
self._id[v] = self._count
for w in G.adj(v):
if not self._marked[w]:
self._dfs(G, w)
def connected(self, v, w):
return self._id[v] == self._id[w]
def id(self, v):
return self._id[v]
def count(self):
return self._count
if __name__ == "__main__":
import sys
from itu.algs4.fundamentals.queue import Queue
from itu.algs4.graphs.graph import Graph
from itu.algs4.stdlib import stdio
from itu.algs4.stdlib.instream import InStream
In = InStream(sys.argv[1])
G = Graph.from_stream(In)
cc = CC(G)
# number of connected components
m = cc.count()
stdio.writef("%i components\n", m)
# compute list of vertices in each connected component
components = [Queue() for _ in range(m)]
for v in range(G.V()):
components[cc.id(v)].enqueue(v)
# print results
for i in range(m):
for v in components[i]:
stdio.writef("%i ", v)
stdio.writeln()
================================================
FILE: itu/algs4/graphs/cycle.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
from itu.algs4.fundamentals.stack import Stack
class Cycle:
"""The Cycle class represents a data type for determining whether an
undirected graph has a cycle. The hasCycle operation determines whether the
graph has a cycle and, if so, the cycle operation returns one.
This implementation uses depth-first search. The constructor takes
time proportional to V + E (in the worst case), where V is the
number of vertices and E is the number of edges. Afterwards, the
hasCycle operation takes constant time the cycle operation takes
time proportional to the length of the cycle.
"""
def __init__(self, G):
"""Determines whether the undirected graph G has a cycle and, if so,
finds such a cycle.
:param G: the undirected graph
"""
if self._has_self_loop(G):
return
if self._has_parallel_edges(G):
return
self._marked = [False] * G.V()
self._edgeTo = [0] * G.V()
self._cycle = None
for v in range(G.V()):
if not self._marked[v]:
self._dfs(G, -1, v)
def _has_self_loop(self, G):
# does this graph have a self loop?
# side effect: initialize cycle to be self loop
for v in range(G.V()):
for w in G.adj(v):
if v == w:
self._cycle = Stack()
self._cycle.push(v)
self._cycle.push(w)
return True
return False
def _has_parallel_edges(self, G):
# does this graph have two parallel edges?
# side effect: initialize cycle to be two parallel edges
self._marked = [False] * G.V()
for v in range(G.V()):
# check for parallel edges incident to v
for w in G.adj(v):
if self._marked[w]:
self._cycle = Stack()
self._cycle.push(v)
self._cycle.push(w)
self._cycle.push(v)
return True
self._marked[w] = True
for w in G.adj(v):
self._marked[w] = False
return False
def has_cycle(self):
"""Returns true if the graph G has a cycle.
:returns: true if the graph has a cycle false otherwise
"""
return self._cycle is not None
def cycle(self):
"""Returns a cycle in the graph G.
:returns: a cycle if the graph G has a cycle,
and null otherwise
"""
return self._cycle
def _dfs(self, G, u, v):
self._marked[v] = True
for w in G.adj(v):
# short circuit if cycle already found
if self._cycle is not None:
return
if not self._marked[w]:
self._edgeTo[w] = v
self._dfs(G, v, w)
elif w != u:
self._cycle = Stack()
x = v
while x != w:
self._cycle.push(x)
x = self._edgeTo[x]
self._cycle.push(w)
self._cycle.push(v)
if __name__ == "__main__":
import sys
from itu.algs4.graphs.graph import Graph
from itu.algs4.stdlib import stdio
from itu.algs4.stdlib.instream import InStream
In = InStream(sys.argv[1])
G = Graph.from_stream(In)
finder = Cycle(G)
if finder.has_cycle():
for v in finder.cycle():
stdio.writef("%i ", v)
stdio.writeln()
else:
stdio.writeln("Graph is acyclic")
================================================
FILE: itu/algs4/graphs/degrees_of_separation.py
================================================
from itu.algs4.graphs.breadth_first_paths import BreadthFirstPaths
from itu.algs4.graphs.symbol_graph import SymbolGraph
from itu.algs4.stdlib import stdio
class DegreesOfSeparation:
"""The DegreesOfSeparation class provides a client for finding the degree
of separation between one distinguished individual and every other
individual in a social network. As an example, if the social network
consists of actors in which two actors are connected by a link if they
appeared in the same movie, and Kevin Bacon is the distinguished
individual, then the client computes the Kevin Bacon number of every actor
in the network.
The running time is proportional to the number of individuals and
connections in the network. If the connections are given implicitly,
as in the movie network example (where every two actors are
connected if they appear in the same movie), the efficiency of the
algorithm is improved by allowing both movie and actor vertices and
connecting each movie to all of the actors that appear in that
movie.
"""
def __init__(self):
"""this class shouldn't be instantiated."""
pass
@staticmethod
def main(args):
"""Reads in a social network from a file, and then repeatedly reads in
individuals from standard input and prints out their degrees of
separation. Takes three command-line arguments: the name of a file, a
delimiter, and the name of the distinguished individual. Each line in
the file contains the name of a vertex, followed by a list of the names
of the vertices adjacent to that vertex, separated by the delimiter.
:param args: the command-line arguments
"""
filename = args[1]
delimiter = args[2]
source = args[3]
sg = SymbolGraph(filename, delimiter)
G = sg.graph()
if not sg.contains(source):
stdio.writeln("{} not in database".format(source))
return
s = sg.index_of(source)
bfs = BreadthFirstPaths(G, s)
while not stdio.isEmpty():
sink = stdio.readLine()
if sg.contains(sink):
t = sg.index_of(sink)
if bfs.has_path_to(t):
for v in bfs.path_to(t):
stdio.writef("\t%s\n", sg.name_of(v))
else:
stdio.writeln("\tNot connected")
else:
stdio.writeln("\tNot in database.")
if __name__ == "__main__":
import sys
DegreesOfSeparation.main(sys.argv)
================================================
FILE: itu/algs4/graphs/depth_first_order.py
================================================
from itu.algs4.fundamentals.queue import Queue
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.digraph import Digraph
class DepthFirstOrder:
"""The DepthFirstOrder class represents a data type for determining depth-
first search ordering of the vertices in a digraph or edge-weighted
digraph, including preorder, postorder, and reverse postorder.
This implementation uses depth-first search. The constructor takes time proportional
to V + E (in the worst case), where V is the number of vertices and E is the number
of edges. Afterwards, the preorder, postorder, and reverse postorder operation takes
take time proportional to V.
For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
"""
def __init__(self, digraph):
"""Determines a depth-first order for the digraph.
:param digraph: the digraph to check
"""
self._pre = [0] * digraph.V()
self._post = [0] * digraph.V()
self._preorder = Queue()
self._postorder = Queue()
self._marked = [False] * digraph.V()
self._pre_counter = 0
self._post_counter = 0
if isinstance(digraph, Digraph):
dfs = self._dfs
else:
dfs = self._dfs_edge_weighted
for v in range(digraph.V()):
if not self._marked[v]:
dfs(digraph, v)
def post(self, v=None):
"""Either returns the postorder number of vertex v or, if v is None,
returns the vertices in postorder.
:param v: None, or the vertex to return the postorder number of
:return: if v is None, the vertices in postorder, otherwise the postorder
number of v
"""
if v is None:
return self._postorder
else:
self._validate_vertex(v)
return self._post[v]
def pre(self, v=None):
"""Either returns the preorder number of vertex v or, if v is None,
returns the vertices in preorder.
:param v: None, or the vertex to return the preorder number of
:return: if v is None, the vertices in preorder, otherwise the preorder
number of v
"""
if v is None:
return self._preorder
else:
self._validate_vertex(v)
return self._pre[v]
def reverse_post(self):
"""Returns the vertices in reverse postorder.
:return: the vertices in reverse postorder, as an iterable of vertices
"""
reverse = Stack()
for v in self._postorder:
reverse.push(v)
return reverse
# run DFS in digraph G from vertex v and compute preorder/postorder
def _dfs(self, digraph, v):
self._marked[v] = True
self._pre[v] = self._pre_counter
self._pre_counter += 1
self._preorder.enqueue(v)
for w in digraph.adj(v):
if not self._marked[w]:
self._dfs(digraph, w)
self._postorder.enqueue(v)
self._post[v] = self._post_counter
self._post_counter += 1
# run DFS in edge-weighted digraph G from vertex v and compute preorder/postorder
def _dfs_edge_weighted(self, graph, v):
self._marked[v] = True
self._pre[v] = self._pre_counter
self._pre_counter += 1
self._preorder.enqueue(v)
for edge in graph.adj(v):
w = edge.to_vertex()
if not self._marked[w]:
self._dfs_edge_weighted(graph, w)
self._postorder.enqueue(v)
self._post[v] = self._post_counter
self._post_counter += 1
# throw an IllegalArgumentException unless 0 <= v < V
def _validate_vertex(self, v):
V = len(self._marked)
if v < 0 or v >= V:
raise ValueError("vertex {} is not between 0 and {}", v, V - 1)
# check that pre() and post() are consistent with pre(v) and post(v)
def _check(self):
# check that post(v) is consistent with post()
r = 0
for v in self.post():
if self.post(v) != r:
print("post(v) and post() inconsistent")
return False
r += 1
# check that pre(v) is consistent with pre()
r = 0
for v in self.pre():
if self.pre(v) != r:
print("pre(v) and pre() inconsistent")
return False
r += 1
return True
================================================
FILE: itu/algs4/graphs/depth_first_paths.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
from itu.algs4.fundamentals.stack import Stack
class DepthFirstPaths:
"""The DepthFirstPaths class represents a data type for finding paths from
a source vertex s to every other vertex in an undirected graph.
This implementation uses depth-first search. The constructor takes
time proportional to V + E, where V is the number of vertices and E
is the number of edges. Each call to hasPathTo(int) takes constant
time each call to pathTo(int) takes time proportional to the length
of the path. It uses extra space (not including the graph)
proportional to V.
"""
def __init__(self, G, s):
"""Computes a path between s and every other vertex in graph G.
:param G: the graph
:param s: the source vertex
:raises ValueError: unless 0 <= s < V
"""
self._marked = [False] * G.V() # Has dfs been called for this vertex?
self._edgeTo = [0] * G.V() # last vertex on known path to this vertex
self._s = s # source
self._validateVertex(s)
self._dfs(G, s)
def _dfs(self, G, v):
# depth first search from v
self._marked[v] = True
for w in G.adj(v):
if not self._marked[w]:
self._edgeTo[w] = v
self._dfs(G, w)
def has_path_to(self, v):
"""Is there a path between the source vertex s and vertex v?
:param v: the vertex
:returns: true if there is a path, false otherwise
:raises ValueError: unless 0 <= v < V
"""
self._validateVertex(v)
return self._marked[v]
def path_to(self, v):
"""Returns a path between the source vertex s and vertex v, or None if
no such path.
:param v: the vertex
:returns: the sequence of vertices on a path between the source vertex
s and vertex v, as an Iterable
:raises ValueError: unless 0 <= v < V
"""
self._validateVertex(v)
if not self.has_path_to(v):
return None
path = Stack()
w = v
while w != self._s:
path.push(w)
w = self._edgeTo[w]
path.push(self._s)
return path
def _validateVertex(self, v):
# throw an ValueError unless 0 <= v < V
V = len(self._marked)
if v < 0 or v >= V:
raise ValueError("vertex {} is not between 0 and {}".format(v, V - 1))
if __name__ == "__main__":
import sys
from itu.algs4.graphs.graph import Graph
from itu.algs4.stdlib import stdio
from itu.algs4.stdlib.instream import InStream
In = InStream(sys.argv[1])
G = Graph.from_stream(In)
s = int(sys.argv[2])
dfs = DepthFirstPaths(G, s)
for v in range(G.V()):
if dfs.has_path_to(v):
stdio.writef("%d to %d: ", s, v)
for x in dfs.path_to(v):
if x == s:
stdio.write(x)
else:
stdio.writef("-%i", x)
stdio.writeln()
else:
stdio.writef("%d to %d: not connected\n", s, v)
================================================
FILE: itu/algs4/graphs/depth_first_search.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
class DepthFirstSearch:
"""The DepthFirstSearch class represents a data type for determining the
vertices connected to a given source vertex s in an undirected graph. For
versions that find the paths, see DepthFirstPaths and BreadthFirstPaths.
This implementation uses depth-first search. The constructor takes
time proportional to V + E (in the worst case), where V is the
number of vertices and E is the number of edges. It uses extra space
(not including the graph) proportional to V.
"""
def __init__(self, G, s):
"""Computes the vertices in graph G that are connected to the source
vertex s.
:param G: the graph
:param s: the source vertex
:throws ValueError: unless 0 <= s < V
"""
self._marked = [False] * G.V() # marked[v] = is there an s-v path?
self._count = 0 # number of vertices connected to s
self._validateVertex(s)
self._dfs(G, s)
def _dfs(self, G, v):
# depth first search from v
self._marked[v] = True
self._count += 1
for w in G.adj(v):
if not self._marked[w]:
self._dfs(G, w)
def marked(self, v):
"""Is there a path between the source vertex s and vertex v?
:param v: the vertex
:returns: true if there is a path, false otherwise
:raises ValueError: unless 0 <= v < V
"""
self._validateVertex(v)
return self._marked[v]
def count(self):
"""Returns the number of vertices connected to the source vertex s.
:returns: the number of vertices connected to the source vertex s
"""
return self._count
def _validateVertex(self, v):
# throw an ValueError unless 0 <= v < V
V = len(self._marked)
if v < 0 or v >= V:
raise ValueError("vertex {} is not between 0 and {}".format(v, V - 1))
if __name__ == "__main__":
import sys
from itu.algs4.graphs.graph import Graph
from itu.algs4.stdlib import stdio
from itu.algs4.stdlib.instream import InStream
In = InStream(sys.argv[1])
G = Graph.from_stream(In)
s = int(sys.argv[2])
search = DepthFirstSearch(G, s)
for v in range(G.V()):
if search.marked(v):
stdio.writef("%i ", v)
stdio.writeln()
if search.count() != G.V():
stdio.writeln("NOT connected")
else:
stdio.writeln("connected")
================================================
FILE: itu/algs4/graphs/digraph.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
"""This module implements the directed graph data structure described in
Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
For more information, see chapter 4.2 of the book.
"""
import sys
from itu.algs4.fundamentals.bag import Bag
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.graph import Graph
from itu.algs4.stdlib.instream import InStream
class Digraph:
"""The Graph class represents an undirected graph of vertices.
named 0 through V - 1.
It supports the following two primary operations: add an edge to the graph,
iterate over all of the vertices adjacent to a vertex. It also provides
methods for returning the number of vertices V and the number
of edges E. Parallel edges and self-loops are permitted.
By convention, a self-loop v-v appears in the
adjacency list of v twice and contributes two to the degree
of v.
This implementation uses an adjacency-lists representation, which
is a vertex-indexed array of Bag objects.
All operations take constant time (in the worst case) except
iterating over the vertices adjacent to a given vertex, which takes
time proportional to the number of such vertices.
"""
def __init__(self, V):
"""Initializes an empty graph with V vertices and 0 edges. param V the
number of vertices.
:param V: number of vertices
:raises: ValueError if V < 0
"""
if V < 0:
raise ValueError("Number of vertices must be nonnegative")
self._V = V # number of vertices
self._E = 0 # number of edges
self._adj = [] # adjacency lists
for _ in range(V):
self._adj.append(Bag()) # Initialize all lists to empty bags.
@staticmethod
def from_stream(stream):
"""Initializes a graph from the specified input stream. The format is
the number of vertices V, followed by the number of edges E, followed
by E pairs of vertices, with each entry separated by whitespace.
:param stream: the input stream
:returns: new graph from stream
:raises ValueError: if the endpoints of any edge are not in prescribed range
:raises ValueError: if the number of vertices or edges is negative
:raises ValueError: if the input stream is in the wrong format
"""
V = stream.readInt() # read V
if V < 0:
raise ValueError("Number of vertices must be nonnegative")
g = Digraph(V) # construct this graph
E = stream.readInt() # read E
if E < 0:
raise ValueError("Number of edges in a Graph must be nonnegative")
for _ in range(E):
# Add an edge
v = stream.readInt() # read a vertex,
w = stream.readInt() # read another vertex,
g._validateVertex(v)
g._validateVertex(w)
g.add_edge(v, w) # and add edge connecting them.
return g
@staticmethod
def from_graph(G):
"""Initializes a new graph that is a deep copy of G.
:param G: the graph to copy
:returns: copy of G
"""
g = Graph(G.V())
g._E = G.E()
for v in range(G.V()):
# reverse so that adjacency list is in same order as original
reverse = Stack()
for w in G._adj[v]:
reverse.push(w)
for w in reverse:
g._adj[v].add(w)
def V(self):
"""Returns the number of vertices in this graph.
:returns: the number of vertices in this graph.
"""
return self._V
def E(self):
"""Returns the number of edges in this graph.
:returns: the number of edges in this graph.
"""
return self._E
def _validateVertex(self, v):
# throw a ValueError unless 0 <= v < V
if v < 0 or v >= self._V:
raise ValueError("vertex {} is not between 0 and {}".format(v, self._V))
def add_edge(self, v, w):
"""Adds the undirected edge v-w to this graph.
:param v: one vertex in the edge
:param w: the other vertex in the edge
:raises ValueError: unless both 0 <= v < V and 0 <= w < V
"""
self._adj[v].add(w) # add w to v's list
self._E += 1
def adj(self, v):
"""Returns the vertices adjacent to vertex v.
:param v: the vertex
:returns: the vertices adjacent to vertex v, as an iterable
:raises ValueError: unless 0 <= v < V
"""
self._validateVertex(v)
return self._adj[v]
def degree(self, v):
"""Returns the degree of vertex v.
:param v: the vertex
:returns: the degree of vertex v
:raises ValueError: unless 0 <= v < V
"""
self._validateVertex(v)
return self._adj[v].size()
def reverse(self):
"""Returns the reverse of the digraph.
:returns: the reverse of the digraph
"""
rev = Digraph(self._V)
for v in range(self._V):
for w in self.adj(v):
rev.add_edge(w, v)
return rev
def __repr__(self):
"""Returns a string representation of this graph.
:returns: the number of vertices V, followed by the number of edges E,
followed by the V adjacency lists
"""
s = ["{} vertices, {} edges\n".format(self._V, self._E)]
for v in range(self._V):
s.append("%d : " % (v))
for w in self._adj[v]:
s.append("%d " % (w))
s.append("\n")
return "".join(s)
if __name__ == "__main__":
# Create stream from file or the standard input,
# depending on whether a file name was passed.
stream = sys.argv[1] if len(sys.argv) > 1 else None
d = Digraph.from_stream(InStream(stream))
print(d)
================================================
FILE: itu/algs4/graphs/dijkstra_all_pairs_sp.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
"""This module implements a data type for solving the all-pairs shortest paths
problem in edge-weighted digraphs where the edge weights are nonnegative."""
import sys
from itu.algs4.graphs.dijkstra_sp import DijkstraSP
from itu.algs4.graphs.edge_weighted_digraph import EdgeWeightedDigraph
from itu.algs4.stdlib import instream
class DijkstraAllPairsSP:
"""This implementation runs Dijkstra's algorithm from each vertex. The
constructor takes time proportional to V (E log V) and uses space
proprtional to V2, where V is the number of vertices and E is the number of
edges. Afterwards, the dist() and hasPath() methods take constant time and
the path() method takes time proportional to the number of edges in the
shortest path returned.
For additional documentation, see Section 4.4 of Algorithms, 4th
Edition by Robert Sedgewick and Kevin Wayne.
"""
def __init__(self, edge_weighted_digraph):
"""Computes a shortest paths tree from each vertex to to every other
vertex in the edge-weighted digraph G.
:param edge_weighted_digraph: the edge-weighted digraph
"""
self._all = []
for v in range(edge_weighted_digraph.V()):
self._all.append(DijkstraSP(edge_weighted_digraph, v))
def path(self, source, target):
"""Returns a shortest path from source vertex to the target vertex.
:param source: the source vertex
:param target: the destination vertex
:returns: a shortest path from the source vertex to the target vertex as an iterable of edges,
and None if no such path
"""
self._validateVertex(source)
self._validateVertex(target)
return self._all[source].path_to(target)
def has_path(self, source, target):
"""Is there a path from the source vertex to the target vertex?
:param source: the source vertex
:param target: the target vertex
:returns: True if there is a path from the source to the target, and False otherwise
"""
return self.dist(source, target) < float("inf")
def dist(self, source, target):
"""Returns the length of a shortest path from the source vertex to the
target vertex.
:param source: the source vertex
:param target: the target vertex
:returns: the length of a shortest path from the source vertex to the target vertex;
float('inf') if no such path
"""
self._validateVertex(source)
self._validateVertex(target)
return self._all[source].dist_to(target)
# throw a ValueError unless 0 <= v < V
def _validateVertex(self, v):
V = len(self._all)
if v < 0 or v >= V:
raise ValueError("vertex {} is not between 0 and {}".format(v, (V - 1)))
if __name__ == "__main__":
# Create stream from file or the standard input,
# depending on whether a file name was passed.
stream = sys.argv[1] if len(sys.argv) > 1 else None
# Create a DijkstraAllPairsSP data structure
g = EdgeWeightedDigraph.from_stream(instream.InStream(stream))
dijkstra_all = DijkstraAllPairsSP(g)
# Print the shortest path distances between all possible pairs of vertices.
for source in range(g.V()):
for target in range(g.V()):
print(dijkstra_all.dist(source, target))
================================================
FILE: itu/algs4/graphs/dijkstra_sp.py
================================================
import sys
from itu.algs4.errors.errors import IllegalArgumentException
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.edge_weighted_digraph import EdgeWeightedDigraph
from itu.algs4.sorting.index_min_pq import IndexMinPQ
from itu.algs4.stdlib.instream import InStream
# Created for BADS 2018
# See README.md for details
# Python 3
class DijkstraSP:
"""The DijkstraSP class represents a data type for solving the single-
source shortest paths problem in edge-weighted digraphs where the edge
weights are nonnegative.
This implementation uses Dijkstra's algorithm with a binary heap.
The constructor takes time proportional to E log V, where V is the
number of vertices and E is the number of edges. Each call to
dist_to() and has_path_to() takes constant time. Each call to
path_to() takes time proportional to the number of edges in the
shortest path returned.
"""
def __init__(self, G, s):
"""Computes a shortest-paths tree from the source vertex s to every
other vertex in the edge-weighted digraph G.
:param G: The edge-weighted digraph
:param s: The source vertex
:raises IllegalArgumentException: if an edge weight is negative
:raises IllegalArgumentException: unless 0 <= s < V
"""
for e in G.edges():
if e.weight() < 0:
raise IllegalArgumentException("edge {} has negative weight".format(e))
self._dist_to = [float("inf")] * G.V()
self._edge_to = [None] * G.V()
self._validate_vertex(s)
self._dist_to[s] = 0.0
self._pq = IndexMinPQ(G.V())
self._pq.insert(s, 0.0)
while not self._pq.is_empty():
v = self._pq.del_min()
for e in G.adj(v):
self._relax(e)
def dist_to(self, v):
"""Returns the length of a shortest path from the source vertex s to
vertex v.
:param v: the destination vertex
:return: the length of a shortest path from the source vertex s to vertex v
:rtype: float
:raises IllegalArgumentException: unless 0 <= v < V
"""
self._validate_vertex(v)
return self._dist_to[v]
def has_path_to(self, v):
"""Returns True if there is a ath from the source vertex s to vertex v.
:param v: the destination vertex
:return: True if there is a path from the source vertex
s to vertex v. Otherwise returns False
:rtype: bool
:raises IllegalArgumentException: unless 0 <= v < V
"""
self._validate_vertex(v)
return self._dist_to[v] < float("inf")
def path_to(self, v):
"""Returns a shortest path from the source vertex s to vertex v.
:param v: the destination vertex
:return: a shortest path from the source vertex s to vertex v
:rtype: collections.iterable[DirectedEdge]
:raises IllegalArgumentException: unless 0 <= v < V
"""
self._validate_vertex(v)
if not self.has_path_to(v):
return None
path = Stack()
e = self._edge_to[v]
while e is not None:
path.push(e)
e = self._edge_to[e.from_vertex()]
return path
def _relax(self, e):
"""Relaxes the edge e and updates the pq if changed.
:param e: the edge to relax
"""
v = e.from_vertex()
w = e.to_vertex()
if self._dist_to[w] > self._dist_to[v] + e.weight():
self._dist_to[w] = self._dist_to[v] + e.weight()
self._edge_to[w] = e
if self._pq.contains(w):
self._pq.decrease_key(w, self._dist_to[w])
else:
self._pq.insert(w, self._dist_to[w])
def _validate_vertex(self, v):
"""Raises an IllegalArgumentException unless 0 <= v < V.
:param v: the vertex to be validated
"""
V = len(self._dist_to)
if v < 0 or v >= V:
raise IllegalArgumentException(
"vertex {} is not between 0 and {}".format(v, V - 1)
)
def main():
"""Creates an EdgeWeightedDigraph from input file.
Runs DijkstraSP on the graph with the given source vertex. Prints
the shortest path from the source vertex to all other vertices.
"""
if len(sys.argv) == 3:
stream = InStream(sys.argv[1])
G = EdgeWeightedDigraph.from_stream(stream)
s = int(sys.argv[2])
sp = DijkstraSP(G, s)
for t in range(G.V()):
if sp.has_path_to(t):
print("{} to {} ({:.2f}) ".format(s, t, sp.dist_to(t)), end="")
for e in sp.path_to(t):
print(e, end=" ")
print()
else:
print("{} to {} no path\n".format(s, t))
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/graphs/dijkstra_undirected_sp.py
================================================
import sys
from itu.algs4.errors.errors import IllegalArgumentException
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.edge_weighted_graph import EdgeWeightedGraph
from itu.algs4.sorting.index_min_pq import IndexMinPQ
from itu.algs4.stdlib.instream import InStream
# Created for BADS 2018
# See README.md for details
# Python 3
class DijkstraUndirectedSP:
"""The DijkstraSP class represents a data type for solving the single-
source shortest paths problem in edge-weighted diagraphs where the edge
weights are nonnegative.
This implementation uses Dijkstra's algorithm with a binary heap.
The constructor takes time proportional to E log V, where V is the
number of vertices and E is the number of edges. Each call to
dist_to() and has_path_to() takes constant time each call to
path_to() takes time proportional to the number of edges in the
shortest path returned.
"""
def __init__(self, G, s):
"""Computes a shortest-paths tree from the source vertex s to every
other vertex in the edge-weighted graph G.
:param G: the edge-weighted graph
:param s: the source vertex
:raises IllegalArgumentException: if an edge weight is negative
:raises IllegalArgumentException: unless 0 <= s < V
"""
for e in G.edges():
if e.weight() < 0:
raise IllegalArgumentException("edge {} has negative weight".format(e))
self._dist_to = [float("inf")] * G.V()
self._edge_to = [None] * G.V()
self._dist_to[s] = 0.0
self._validate_vertex(s)
self._pq = IndexMinPQ(G.V())
self._pq.insert(s, 0)
while not self._pq.is_empty():
v = self._pq.del_min()
for e in G.adj(v):
self._relax(e, v)
def dist_to(self, v):
"""Returns the length of a shortest path between the source vertex s
and vertex v.
:param v: the destination vertex
:return: the length of a shortest path between the source vertex s and
the vertex v. float('inf') is not such path
:rtype: float
:raises IllegalArgumentException: unless 0 <= v < V
"""
return self._dist_to[v]
def has_path_to(self, v):
"""Returns true if there is a path between the source vertex s and
vertex v.
:param v: the destination vertex
:return: True if there is a path between the source vertex
s to vertex v. False otherwise
:rtype: bool
"""
return self._dist_to[v] < float("inf")
def path_to(self, v):
"""Returns a shortest path between the source vertex s and vertex v.
:param v: the destination vertex
:return: a shortest path between the source vertex s and vertex v.
None if no such path
:rtype: collections.iterable[Edge]
:raises IllegalArgumentException: unless 0 <= v < V
"""
self._validate_vertex(v)
if not self.has_path_to(v):
return None
path = Stack()
x = v
e = self._edge_to[v]
while e is not None:
edge = self._edge_to[x]
path.push(edge)
x = e.other(x)
e = self._edge_to[x]
return path
def _validate_vertex(self, v):
"""Raises an IllegalArgumentException unless 0 <= v < V.
:param v: the vertex to validate
"""
V = len(self._dist_to)
if v < 0 or v >= V:
raise IllegalArgumentException(
"vertex {} is not between 0 and {}".format(v, V - 1)
)
def _relax(self, e, v):
"""Relax edge e and update pq if changed.
:param e: the edge to relax
:param v: the vertex e goes out from
"""
w = e.other(v)
if self._dist_to[v] + e.weight() < self._dist_to[w]:
self._dist_to[w] = self._dist_to[v] + e.weight()
self._edge_to[w] = e
if self._pq.contains(w):
self._pq.decrease_key(w, self._dist_to[w])
else:
self._pq.insert(w, self._dist_to[w])
def main():
if len(sys.argv) == 3:
stream = InStream(sys.argv[1])
G = EdgeWeightedGraph.from_stream(stream)
s = int(sys.argv[2])
sp = DijkstraUndirectedSP(G, s)
for t in range(G.V()):
if sp.has_path_to(t):
print("{} to {} ({:.2f}) ".format(s, t, sp.dist_to(t)), end="")
for e in sp.path_to(t):
print(e, end=" ")
print()
else:
print("{} to {} no path\n".format(s, t))
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/graphs/directed_cycle.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
"""This module implements the directed cycle algorithm described in Algorithms,
4th Edition by Robert Sedgewick and Kevin Wayne. This version works for both
weighted and unweighted directed graphs, due to Python's duck-typing.
For more information, see chapter 4.2 of the book.
"""
import sys
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.digraph import Digraph
from itu.algs4.stdlib.instream import InStream
class DirectedCycle:
"""The DirectedCycle class represents a data type for determining whether a
digraph has a directed cycle. The hasCycle operation determines whether the
digraph has a directed cycle and, and of so, the cycle operation returns
one.
This implementation uses depth-first search. The constructor takes time proportional
to V + E (in the worst case), where V is the number of vertices and E is the
number of edges. Afterwards, the hasCycle operation takes constant time; the
cycle operation takes time proportional to the length of the cycle.
See Topological to compute a topological order if the digraph is acyclic.
For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
"""
def __init__(self, digraph):
"""Determines whether the digraph has a directed cycle and, if so,
finds such a cycle.
:digraph: the digraph
"""
self._cycle = None
self._on_stack = [False] * digraph.V()
self._edge_to = [0] * digraph.V()
self._marked = [False] * digraph.V()
for v in range(digraph.V()):
if not self._marked[v]:
self._dfs(digraph, v)
# check that algorithm computes either the topological order or finds a directed cycle
def _dfs(self, digraph, v):
self._on_stack[v] = True
self._marked[v] = True
for w in digraph.adj(v):
# short circuit if directed cycle found
if self.has_cycle():
return
# found new vertex, so recur
elif not self._marked[w]:
self._edge_to[w] = v
self._dfs(digraph, w)
# trace back directed cycle
elif self._on_stack[w]:
self._cycle = Stack()
x = v
while x != w:
self._cycle.push(x)
x = self._edge_to[x]
self._cycle.push(w)
self._cycle.push(v)
self._on_stack[v] = False
def has_cycle(self):
"""Does the digraph have a directed cycle?
:returns: true if there is a cycle, false otherwise
"""
return self._cycle is not None
def cycle(self):
"""Returns a directed cycle if the digraph has a directed cycle, and
null otherwise.
:returns: a directed cycle (as an iterable) if the digraph has a directed cycle, and null otherwise
"""
return self._cycle
if __name__ == "__main__":
# Create stream from file or the standard input,
# depending on whether a file name was passed.
stream = sys.argv[1] if len(sys.argv) > 1 else None
d = Digraph.from_stream(InStream(stream))
cyc = DirectedCycle(d)
print(cyc.cycle())
================================================
FILE: itu/algs4/graphs/directed_dfs.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
import sys
from itu.algs4.fundamentals.bag import Bag
from itu.algs4.graphs.digraph import Digraph
from itu.algs4.stdlib.instream import InStream
class DirectedDFS:
"""The DirectedDFS class represents a data type for determining the vertices
reachable from a given source vertex s (or a set of source vertices) in a
digraph. For versions that find the paths, see DepthFirstDirectedPaths and
BreadthFirstDirectedPaths.
This implementation uses depth-first search.
The constructor takes time proportional to V + E (in the worst case),
where V is the number of vertices and E is the number of edges.
For additional documentation, see Section 4.2 of Algorithms,
4th Edition by Robert Sedgewick and Kevin Wayne.
"""
def __init__(self, G, *s):
"""Computes the vertices in digraph G that are reachable from the source
vertex s.
:param G: the digraph
:param s: the source vertex/vertices
:raises ValueError: unless 0 <= s_ < V for every s_ in s
"""
self.marked = [False for i in range(0, G.V())]
self.reachables = 0
for s_ in s:
self._validate_vertex(s_)
self._dfs(G, s_)
def _dfs(self, G, v):
self.reachables += 1
self.marked[v] = True
for w in G.adj(v):
if not self.marked[w]:
self._dfs(G, w)
def is_marked(self, v):
"""Is there a directed path from the source vertex and vertex v?
:param v: the vertex
:returns: True if there is a directed
"""
self._validate_vertex(v)
return self.marked[v]
def count(self):
"""
Returns the number of vertices reachable from the source vertex
(or source vertices)
:returns: the number of vertices reachable from the source vertex
(or source vertices)
"""
return self.reachables
def _validate_vertex(self, v):
# Raise a ValueError unless 0 <= v < V
V = len(self.marked)
if v < 0 or v >= V:
raise ValueError("vertex {} is not between 0 and {}".format(v, V - 1))
def main():
"""Unit tests the DirectedDFS data type."""
G = Digraph.from_stream(InStream(None))
sources = Bag()
for i in range(1, len(sys.argv)):
s = int(sys.argv[i])
sources.add(s)
dfs = DirectedDFS(G, *sources)
print("Reachable vertices:")
for v in range(0, G.V()):
if dfs.is_marked(v):
print(v, end=" ")
print()
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/graphs/directed_edge.py
================================================
import math
from itu.algs4.errors.errors import IllegalArgumentException
# Created for BADS 2018
# See README.md for details
# Python 3
class DirectedEdge:
"""The DirectedEdge class represents a weighted edge in an
EdgeWeightedDigraph.
Each edge consists of two integers (naming the two vertices) and a
real-value weight. The data type provides methods for accessing the
two endpoints of the directed edge and the weight.
"""
def __init__(self, v, w, weight):
"""Initializes a directed edge from vertex v to vertex w with the given
weight.
:param v: the tail vertex
:param w: the head vertex
:param weight: the weight of the directed edge
:raises IllegalArgumentException: if either v or w is a negative integer
:raises IllegalArgumentException: if weight is NaN
"""
if v < 0:
raise IllegalArgumentException("Vertex names must be nonnegative integers")
if w < 0:
raise IllegalArgumentException("Vertex names must be nonnegative integers")
if math.isnan(weight):
raise IllegalArgumentException("Weight is NaN")
self._v = v
self._w = w
self._weight = weight
def from_vertex(self):
"""Returns the tail vertex of the directed edge.
:return: the tail vertex of the directed edge
:rtype: int
"""
return self._v
def to_vertex(self):
"""Returns the head vertex of the directed edge.
:return: the head vertex of the directed edge
:rtype: int
"""
return self._w
def weight(self):
"""Returns the weight of the directed edge.
:return: the weight of the directed edge
:rtype: float
"""
return self._weight
def __repr__(self):
"""Returns a string representation of the directed edge.
:return: a string representation of the directed edge
:rtype: str
"""
return "{}->{} {:5.2f}".format(self._v, self._w, self._weight)
def main():
"""Creates a directed edge and prints it.
:return:
"""
e = DirectedEdge(12, 34, 5.67)
print(e)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/graphs/edge.py
================================================
import math
from itu.algs4.errors.errors import IllegalArgumentException
# Created for BADS 2018
# See README.md for details
# Python 3
class Edge:
"""The Edge class represents a weighted edge in an EdgeWeightedGraph.
Each edge consists of two integers (naming the two vertices) and a
real-value weight. The data type provides methods for accessing the
two endpoints of the edge and the weight. The natural order for this
data type is by ascending order of weight.
"""
def __init__(self, v, w, weight):
"""Initializes an edge between vertices v and w of the given weight.
:param v: one vertex
:param w: the other vertex
:param weight: the weight of this edge
:raises IllegalArgumentException: if either v or w is a negative integer
:raises IllegalArgumentException: if weight is NaN
"""
if v < 0:
raise IllegalArgumentException("vertex index must be a nonnegative integer")
if w < 0:
raise IllegalArgumentException("vertex index must be a nonnegative integer")
if math.isnan(weight):
raise IllegalArgumentException("Weight is NaN")
self._v = v
self._w = w
self._weight = weight
def weight(self):
"""Returns the weight of this edge.
:return: the weight of this edge
:rtype: float
"""
return self._weight
def either(self):
"""Returns either endpoint of this edge.
:return: either endpoint of this edge
:rtype: int
"""
return self._v
def other(self, vertex):
"""Returns the endpoint of this edge that is different from the given
vertex.
:param vertex: one endpoint of this edge
:return: the other endpoint of this edge
:rtype: int
:raises IllegalArgumentException: if the vertex is not one of the endpoints of this edge
"""
if vertex == self._v:
return self._w
elif vertex == self._w:
return self._v
else:
raise IllegalArgumentException("Illegal endpoint")
def __lt__(self, other):
"""Checks if this edge has smaller weight than other edge.
:param other: the edge to compare with
:return: True if weight of this edge is less than weight of other edge otherwise returns False
"""
return self.weight() < other.weight()
def __gt__(self, other):
"""Checks if this edge has greater weight than other edge.
:param other: the edge to compare with
:return: True if weight of this edge is greater than weight of other edge otherwise returns False
"""
return self.weight() > other.weight()
def __repr__(self):
"""Returns a string representation of this edge.
:return: a string representation of this edge
"""
return "{}-{} {:.5f}".format(self._v, self._w, self._weight)
def main():
"""Creates an edge and prints it."""
e = Edge(12, 34, 5.67)
print(e)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/graphs/edge_weighted_digraph.py
================================================
import sys
from itu.algs4.errors.errors import IllegalArgumentException
from itu.algs4.fundamentals.bag import Bag
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.directed_edge import DirectedEdge
from itu.algs4.stdlib.instream import InStream
# Created for BADS 2018
# See README.md for details
# Python 3
class EdgeWeightedDigraph:
"""The EdgeWeightedDigraph class represents an edge-weighted digraph of
vertices named 0 through V-1, where each directed edge is of type
DirectedEdge and has a real-valued weight.
It supports the following two primary operations: add a directed
edge to the digraph and iterate over all edges incident from a given
vertex. it also provides methods for returning the number of
vertices V and the number of edges E. Parallel edges and self-loops
are permitted. This implementation uses an adjacency-lists
representation, which is a vertex-indexed array of Bag objects. All
operations take constant time (in the worst case) except iterating
over the edges incident from a given vertex, which takes time
proportional to the number of such edges.
"""
def __init__(self, V):
"""Initializes an empty edge-weighted digraph with V vertices and 0
edges.
:param V: the number of vertices
:raises IllegalArgumentException: if V < 0
"""
if V < 0:
raise IllegalArgumentException(
"Number of vertices in a Digraph must be nonnegative"
)
self._V = V
self._E = 0
self._indegree = [0] * V
self._adj = [None] * V
for v in range(V):
self._adj[v] = Bag()
@staticmethod
def from_graph(G):
"""Initializes a new edge-weighted digraph that is a deep copy of G.
:param G: the edge-weighted digraph to copy
:return: a copy of graph G
:rtype: EdgeWeightedDigraph
"""
g = EdgeWeightedDigraph(G.V())
g._E = G.E()
for v in range(G.V()):
g._indegree[v] = G.indegree(v)
reverse = Stack()
for e in G.adj(v):
reverse.push(e)
for e in reverse:
g._adj[v].add(e)
return g
@staticmethod
def from_stream(stream):
"""Initializes an edge-weighted digraph from the specified input
stream. The format is the number of vertices V, followed by the number
of edges E, followed by E pairs of vertices and edge weights, with each
entry seperated by whitespace.
:param stream: the input stream
:raises IllegalArgumentException: if the endpoints of any edge are not in prescribed range
:raises IllegalArgumentException: if the number of vertices or edges is negative
:return: the edge-weighted digraph
:rtype: EdgeWeightedDigraph
"""
g = EdgeWeightedDigraph(stream.readInt())
E = stream.readInt()
if g._E < 0:
raise IllegalArgumentException("Number of edges must be nonnegative")
for _ in range(E):
v = stream.readInt()
w = stream.readInt()
g._validate_vertex(v)
g._validate_vertex(w)
weight = stream.readFloat()
g.add_edge(DirectedEdge(v, w, weight))
return g
def V(self):
"""Returns the number of vertices in this edge-weighted digraph.
:return: the number of vertices in this edge-weighted digraph
:rtype: int
"""
return self._V
def E(self):
"""Returns the number of edges in this edge-weighted digraph.
:return: the number of edges in this edge-weighted digraph
:rtype: int
"""
return self._E
def _validate_vertex(self, v):
"""Raises an IllegalArgumentException unluess 0 <= v < V.
:param v: the vertex to validate
"""
if v < 0 or v >= self._V:
raise IllegalArgumentException(
"vertex {} is not between 0 and {}".format(v, self._V - 1)
)
def add_edge(self, e):
"""Adds the directed edge e to this edge-weighted digraph.
:param e: the edge
:raises IllegalArgumentException: unless endpoints of edge are between 0 and V-1
"""
v = e.from_vertex()
w = e.to_vertex()
self._validate_vertex(v)
self._validate_vertex(w)
self._adj[v].add(e)
self._indegree[w] += 1
self._E += 1
def adj(self, v):
"""Returns the directed edges incident from vertex v.
:param v: the vertex
:return: the directed edges incident from vertex v.
:rtype: collections.iterable[DirectedEdge]
:raises IllegalArgumentException: unless 0 <= v < V
"""
self._validate_vertex(v)
return self._adj[v]
def outdegree(self, v):
"""Returns the number of directed edges incident from vertex v. This is
known as the outdegree of vertex v.
:param v: the vertex
:return: the outdegree of vertex v
:rtype: int
:raises IllegalArgumentException: unless 0 <= v < V
"""
self._validate_vertex(v)
return self._adj[v].size()
def indegree(self, v):
"""Returns the number of directed edges incident to vertex v. This is
known as the indegree of vertex v.
:param v: the vertex
:return: the indegree of vertex v
:rtype: int
:raises IllegalArgumentException: unless 0 <= v < V
"""
self._validate_vertex(v)
return self._indegree[v]
def edges(self):
"""Returns all directed edges in this edge-weighted digraph.
:return: all edges in this edge-weighted digraph
:rtype: collections.iterable[DirectedEdge]
"""
edges = Bag()
for v in range(self._V):
for e in self._adj[v]:
edges.add(e)
return edges
def __repr__(self):
"""Returns a string representation of this edge-weighted digraph.
:return: the number of vertices V, followed by the number of edges E,
followed by the V adjacency lists of edges.
:rtype: str
"""
s = ["{} {} \n".format(self._V, self._E)]
for v in range(self._V):
s.append("{}: ".format(v))
for e in self._adj[v]:
s.append("{} ".format(e))
s.append("\n")
return "".join(s)
def main():
"""Creates an edge-weighted digraph from the given input file and prints
it."""
if len(sys.argv) > 1:
stream = InStream(sys.argv[1])
G = EdgeWeightedDigraph.from_stream(stream)
print(G)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/graphs/edge_weighted_directed_cycle.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
import sys
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.directed_edge import DirectedEdge
from itu.algs4.graphs.edge_weighted_digraph import EdgeWeightedDigraph
# Execution: python edge_weighted_directed_cycle V E F
# Finds a directed cycle in an edge-weighted digraph.
# Runs in O(E + V) time.
"""
The EdgeWeightedDirectedCycle class represents a data type for
determining whether an edge-weighted digraph has a directed cycle.
The hasCycle operation determines whether the edge-weighted
digraph has a directed cycle and, if so, the cycle operation
returns one.
This implementation uses depth-first search.
The constructor takes time proportional to V + E
(in the worst case),
where V is the number of vertices and E is the number of edges.
Afterwards, the hasCycle operation takes constant time;
the cycle operation takes time proportional
to the length of the cycle.
"""
class EdgeWeightedDirectedCycle:
"""Determines whether the edge-weighted digraph G has a directed cycle and,
if so, finds such a cycle.
:param G the edge-weighted digraph
"""
def __init__(self, G):
self._marked = [False] * G.V() # marked[v] = has vertex v been marked?
self._edgeTo = [None] * G.V() # edgeTo[v] = previous DirectedEdge on path to v
self._onStack = [False] * G.V() # onStack[v] = is vertex on the stack?
self._cycle = None # directed cycle (or None if no such cycle)
for v in range(G.V()):
if not self._marked[v]:
self._dfs(G, v)
# check that digraph has a cycle
assert self._check()
# check that algorithm computes either the topological order or finds a directed cycle
def _dfs(self, G, v):
self._onStack[v] = True
self._marked[v] = True
for e in G.adj(v):
w = e.to_vertex()
# short circuit if directed cycle found
if self._cycle is not None:
return
# found new vertex, so recur
elif not self._marked[w]:
self._edgeTo[w] = e
self._dfs(G, w)
# trace back directed cycle
elif self._onStack[w]:
self._cycle = Stack()
f = e
while f.from_vertex() != w:
self._cycle.push(f)
f = self._edgeTo[f.from_vertex()]
self._cycle.push(f)
return
self._onStack[v] = False
# Does the edge-weighted digraph have a directed cycle?
# @return True if the edge-weighted digraph has a directed cycle,
# False otherwise
def has_cycle(self):
return self._cycle is not None
# Returns a directed cycle if the edge-weighted digraph has a directed cycle,
# and None otherwise.
# @return a directed cycle (as an iterable) if the edge-weighted digraph
# has a directed cycle, and None otherwise
def cycle(self):
return self._cycle
# certify that digraph is either acyclic or has a directed cycle
def _check(self):
# edge-weighted digraph is cyclic
if self.has_cycle():
# verify cycle
first = None
last = None
for e in self.cycle():
if first is None:
first = e
if last is not None:
if last.to_vertex() != e.from_vertex():
print("cycle edges {} and {} not incident".format(last, e))
return False
last = e
if last.to_vertex() != first.from_vertex():
print("cycle edges {} and {} not incident".format(last, first))
return False
return True
def main(args):
from itu.algs4.stdlib import stdrandom as stdrandom
# create random DAG with V vertices and E edges; then add F random edges
V = int(args[0])
E = int(args[1])
F = int(args[2])
G = EdgeWeightedDigraph(V)
vertices = [i for i in range(V)]
stdrandom.shuffle(vertices)
for _ in range(E):
while True:
v = stdrandom.uniformInt(0, V)
w = stdrandom.uniformInt(0, V)
if v >= w:
break
weight = stdrandom.uniformFloat(0.0, 1.0)
G.add_edge(DirectedEdge(v, w, weight))
# add F extra edges
for _ in range(F):
v = stdrandom.uniformInt(0, V)
w = stdrandom.uniformInt(0, V)
weight = stdrandom.uniformFloat(0.0, 1.0)
G.add_edge(DirectedEdge(v, w, weight))
print(G)
# find a directed cycle
finder = EdgeWeightedDirectedCycle(G)
if finder.has_cycle():
print("Cycle: ")
for e in finder.cycle():
print("{} ".format(e), end="")
print()
# or give topologial sort
else:
print("No directed cycle")
if __name__ == "__main__":
main(sys.argv[1:])
================================================
FILE: itu/algs4/graphs/edge_weighted_directed_cycle_anton.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
"""This module implements the directed cycle algorithm for EdgeWeightedDigraphs
described in Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne. This
version works for both weighted and unweighted directed graphs, due to Python's
duck-typing.
For more information, see chapter 4.2 of the book.
"""
import sys
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.edge_weighted_digraph import EdgeWeightedDigraph
from itu.algs4.stdlib import instream
class EdgeWeightedDirectedCycle:
"""The EdgeWeightedDirectedCycle class represents a data type for
determining whether edge-weighted digraph has a directed cycle. The
hasCycle operation determines whether the edge-weighted digraph has a
directed cycle and, if so, the cycle operation returns one.
This implementation uses depth-first search. The constructor takes time proportional to
V + E (in the worst case), where V is the number of vertices and E is the number of edges.
Afterwards, the hasCycle operation takes constant time; the cycle operation takes time
proportional to the length of the cycle.
See Topological to compute a topological order if the edge-weighted digraph is acyclic.
For additional documentation, see Section 4.4 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
"""
def __init__(self, edge_weighted_digraph):
"""Determines whether the edge weighted digraph has a directed cycle
and, if so, finds such a cycle.
:digraph: the digraph
"""
self._cycle = None
self._on_stack = [False] * edge_weighted_digraph.V()
self._edge_to = [None] * edge_weighted_digraph.V()
self._marked = [False] * edge_weighted_digraph.V()
for v in range(edge_weighted_digraph.V()):
if not self._marked[v]:
self._dfs(edge_weighted_digraph, v)
# check that algorithm computes either the topological order or finds a directed cycle
def _dfs(self, graph, v):
self._on_stack[v] = True
self._marked[v] = True
for edge in graph.adj(v):
w = edge.to_vertex()
# short circuit if directed cycle found
if self.has_cycle():
return
# found new vertex, so recur
elif not self._marked[w]:
self._edge_to[w] = edge
self._dfs(graph, w)
# trace back directed cycle
elif self._on_stack[w]:
self._cycle = Stack()
f = edge
while f.from_vertex() != w:
self._cycle.push(f)
f = self._edge_to[f.from_vertex()]
self._cycle.push(f)
self._on_stack[v] = False
def has_cycle(self):
"""Does the edge weighted digraph have a directed cycle?
:returns: true if there is a cycle, false otherwise
"""
return self._cycle is not None
def cycle(self):
"""Returns a directed cycle if the edge weighted digraph has a directed
cycle, and null otherwise.
:returns: a directed cycle (as an iterable) if the digraph has a directed cycle, and null otherwise
"""
return self._cycle
if __name__ == "__main__":
# Create stream from file or the standard input,
# depending on whether a file name was passed.
stream = sys.argv[1] if len(sys.argv) > 1 else None
d = EdgeWeightedDigraph.from_stream(instream.InStream(stream))
cyc = EdgeWeightedDirectedCycle(d)
print(cyc.cycle())
================================================
FILE: itu/algs4/graphs/edge_weighted_graph.py
================================================
import sys
from itu.algs4.errors.errors import IllegalArgumentException
from itu.algs4.fundamentals.bag import Bag
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.edge import Edge
from itu.algs4.stdlib.instream import InStream
# Created for BADS 2018
# See README.md for details
# Python 3
class EdgeWeightedGraph:
"""The EdgeWeightedGraph class represents an edge-weighted graph of
vertices named 0 through V-1, where each undirected edge is of type Edge
and has a real-valued weight.
It supports the following two primary operations: add an edge to the
graph, iterate over all of the edges incident to a vertex. It also
provides methods for returning the number of vertices V and the
number of edges E. Parallel edges and self-loops are permitted. By
convention, a self-loop v-v appears in the adjacency list of v twice
and contributes two to the degree of v. This implementation uses an
adjacency-list representation, which is a vertex-indexed array of
Bag objects. All operations take constant time (in the worst case)
except iterating over the edges incident to a given vertex, which
takes time proportional to the number of such edges.
"""
def __init__(self, V):
"""Initializes an empty edge-weighted graph with V vertices and 0
edges.
:param V: the number of vertices
:raises IllegalArgumentException: if V < 0
"""
if V < 0:
raise IllegalArgumentException("Number of vertices must be nonnegative")
self._V = V
self._E = 0
self._adj = [None] * V
for v in range(V):
self._adj[v] = Bag()
@staticmethod
def from_graph(G):
"""Initializes a new edge-weighted graph that is a deep copy of G.
:param G: the edge-weighted graph to copy
:return: the copy of the graph edge-weighted graph G
:rtype: EdgeWeightedGraph
"""
g = EdgeWeightedGraph(G.V())
g._E = G.E()
for v in range(G.V()):
reverse = Stack()
for e in G.adj(v):
reverse.push(e)
for e in reverse:
g._adj[v].add(e)
return g
@staticmethod
def from_stream(stream):
"""Initializes an edge-weighted graph from an input stream. The format
is the number of vertices V, followed by the number of edges E,
followed by E pairs of vertices and edge weights, with each entry
separated by whitespace.
:param stream: the input stream
:raises IllegalArgumentException: if the endpoints of any edge are not in prescribed range
:raises IllegalArgumentException: if the number of vertices or edges is negative
:return: the edge-weighted graph
:rtype: EdgeWeightedGraph
"""
g = EdgeWeightedGraph(stream.readInt())
E = stream.readInt()
if E < 0:
raise IllegalArgumentException("Number of edges must be nonnegative")
for _ in range(E):
v = stream.readInt()
w = stream.readInt()
g._validate_vertex(v)
g._validate_vertex(w)
weight = stream.readFloat()
e = Edge(v, w, weight)
g.add_edge(e)
return g
def add_edge(self, e):
"""Adds the undirected edge e to this edge-weighted graph.
:param e: the edge
"""
v = e.either()
w = e.other(v)
self._validate_vertex(v)
self._validate_vertex(w)
self._adj[v].add(e)
self._adj[w].add(e)
self._E += 1
def adj(self, v):
"""Returns the edges incident on vertex v.
:param v: the vertex
:return: the edges incident on vertex v
:rtype: collections.iterable[Edge]
"""
self._validate_vertex(v)
return self._adj[v]
def V(self):
"""Returns the number of vertices in this edge-weighted graph.
:return: the number of vertices in this edge-weighted graph
:rtype: int
"""
return self._V
def E(self):
"""Returns the number of edges in this edge-weighted graph.
:return: the number of edges in this edge-weighted graph
:rtype: int
"""
return self._E
def degree(self, v):
"""Returns the degree of vertex v.
:param v: the vertex
:return: the degree of vertex v
:rtype: int
:raises IllegalArgumentException: unless 0 <= v < V
"""
self._validate_vertex(v)
return self._adj[v].size()
def edges(self):
"""Returns all edges in this edge-weighted graph.
:return: all edges in this edge-weighted graph
"""
edges = Bag()
for v in range(self._V):
self_loops = 0
for e in self.adj(v):
if e.other(v) > v:
edges.add(e)
elif e.other(v) is v:
if self_loops % 2 == 0:
edges.add(e)
self_loops += 1
return edges
def _validate_vertex(self, v):
"""Raises an IllegalArgumentException unless 0 <= v < V.
:param v: the vertex to be validated
"""
if v < 0 or v >= self._V:
raise IllegalArgumentException(
"vertex {} is not between 0 and {}".format(v, self._V - 1)
)
def __repr__(self):
"""Returns a string representation of the edge-weighted graph.
This method takes time proportional to E + V.
:return: the number of vertices, followed by the number of edges,
followed by the V adjacency lists of edges
"""
s = ["{} {} \n".format(self._V, self._E)]
for v in range(self._V):
s.append("{}: ".format(v))
for e in self._adj[v]:
s.append("{}: ".format(e))
s.append("\n")
return "".join(s)
def main():
"""Creates an edge-weighted graph from the given input file and prints
it."""
if len(sys.argv) > 1:
stream = InStream(sys.argv[1])
G = EdgeWeightedGraph.from_stream(stream)
print(G)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/graphs/graph.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
from itu.algs4.fundamentals.bag import Bag
from itu.algs4.fundamentals.stack import Stack
class Graph:
"""The Graph class represents an undirected graph of vertices.
named 0 through V - 1.
It supports the following two primary operations: add an edge to the graph,
iterate over all of the vertices adjacent to a vertex. It also provides
methods for returning the number of vertices V and the number
of edges E. Parallel edges and self-loops are permitted.
By convention, a self-loop v-v appears in the
adjacency list of v twice and contributes two to the degree
of v.
This implementation uses an adjacency-lists representation, which
is a vertex-indexed array of Bag objects.
All operations take constant time (in the worst case) except
iterating over the vertices adjacent to a given vertex, which takes
time proportional to the number of such vertices.
"""
def __init__(self, V):
"""Initializes an empty graph with V vertices and 0 edges. param V the
number of vertices.
:param V: number of vertices
:raises: ValueError if V < 0
"""
if V < 0:
raise ValueError("Number of vertices must be nonnegative")
self._V = V # number of vertices
self._E = 0 # number of edges
self._adj = [] # adjacency lists
for _ in range(V):
self._adj.append(Bag()) # Initialize all lists to empty bags.
@staticmethod
def from_stream(stream):
"""Initializes a graph from the specified input stream. The format is
the number of vertices V, followed by the number of edges E, followed
by E pairs of vertices, with each entry separated by whitespace.
:param stream: the input stream
:returns: new graph from stream
:raises ValueError: if the endpoints of any edge are not in prescribed range
:raises ValueError: if the number of vertices or edges is negative
:raises ValueError: if the input stream is in the wrong format
"""
V = stream.readInt() # read V
if V < 0:
raise ValueError("Number of vertices must be nonnegative")
g = Graph(V) # construct this graph
E = stream.readInt() # read E
if E < 0:
raise ValueError("Number of edges in a Graph must be nonnegative")
for _ in range(E):
# Add an edge
v = stream.readInt() # read a vertex,
w = stream.readInt() # read another vertex,
g._validateVertex(v)
g._validateVertex(w)
g.add_edge(v, w) # and add edge connecting them.
return g
@staticmethod
def from_graph(G):
"""Initializes a new graph that is a deep copy of G.
:param G: the graph to copy
:returns: copy of G
"""
g = Graph(G.V())
g._E = G.E()
for v in range(G.V()):
# reverse so that adjacency list is in same order as original
reverse = Stack()
for w in G._adj[v]:
reverse.push(w)
for w in reverse:
g._adj[v].add(w)
def V(self):
"""Returns the number of vertices in this graph.
:returns: the number of vertices in this graph.
"""
return self._V
def E(self):
"""Returns the number of edges in this graph.
:returns: the number of edges in this graph.
"""
return self._E
def _validateVertex(self, v):
# throw a ValueError unless 0 <= v < V
if v < 0 or v >= self._V:
raise ValueError("vertex {} is not between 0 and {}".format(v, self._V))
def add_edge(self, v, w):
"""Adds the undirected edge v-w to this graph.
:param v: one vertex in the edge
:param w: the other vertex in the edge
:raises ValueError: unless both 0 <= v < V and 0 <= w < V
"""
self._adj[v].add(w) # add w to v's list
self._adj[w].add(v) # add v to w's list
self._E += 1
def adj(self, v):
"""Returns the vertices adjacent to vertex v.
:param v: the vertex
:returns: the vertices adjacent to vertex v, as an iterable
:raises ValueError: unless 0 <= v < V
"""
self._validateVertex(v)
return self._adj[v]
def degree(self, v):
"""Returns the degree of vertex v.
:param v: the vertex
:returns: the degree of vertex v
:raises ValueError: unless 0 <= v < V
"""
self._validateVertex(v)
return self._adj[v].size()
def __repr__(self):
"""Returns a string representation of this graph.
:returns: the number of vertices V, followed by the number of edges E,
followed by the V adjacency lists
"""
s = ["{} vertices, {} edges\n".format(self._V, self._E)]
for v in range(self._V):
s.append("%d : " % (v))
for w in self._adj[v]:
s.append("%d " % (w))
s.append("\n")
return "".join(s)
if __name__ == "__main__":
import sys
from itu.algs4.stdlib import stdio
from itu.algs4.stdlib.instream import InStream
In = InStream(sys.argv[1])
G = Graph.from_stream(In)
stdio.writeln(G)
================================================
FILE: itu/algs4/graphs/kosaraju_sharir_scc.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
"""
* Execution: python kosaraju_sharir_scc.py filename.txt
* Dependencies: Digraph TransitiveClosure InStream DepthFirstOrder
* Data files: https:#algs4.cs.princeton.edu/42digraph/tinyDG.txt
* https:#algs4.cs.princeton.edu/42digraph/mediumDG.txt
* https:#algs4.cs.princeton.edu/42digraph/largeDG.txt
*
* Compute the strongly-connected components of a digraph using the
* Kosaraju-Sharir algorithm.
*
* Runs in O(E + V) time.
*
* % python kosaraju_sharir_scc.py tinyDG.txt
* 5 strong components
* 1
* 0 2 3 4 5
* 9 10 11 12
* 6 8
* 7
*
"""
import sys
from itu.algs4.errors.errors import IllegalArgumentException
from itu.algs4.fundamentals.queue import Queue
from itu.algs4.graphs.depth_first_order import DepthFirstOrder
from itu.algs4.graphs.digraph import Digraph
from itu.algs4.graphs.transitive_closure import TransitiveClosure
from itu.algs4.stdlib.instream import InStream
class KosarajuSharirSCC:
"""
* Computes the strong components of the digraph G.
* @param G the digraph
"""
def __init__(self, G):
self._marked = [False] * G.V() # marked[v] = has vertex v been visited?
self._id = [0] * G.V() # id[v] = id of strong component containing v
self._count = 0 # number of strongly-connected components
# compute reverse postorder of reverse graph
dfo = DepthFirstOrder(G.reverse())
# run DFS on G, using reverse postorder to guide calculation
for v in dfo.reverse_post():
if not self._marked[v]:
self._dfs(G, v)
self._count += 1
# check that id[] gives strong components
assert self._check(G)
# DFS on graph G
def _dfs(self, G, v):
self._marked[v] = True
self._id[v] = self._count
for w in G.adj(v):
if not self._marked[w]:
self._dfs(G, w)
"""
* Returns the number of strong components.
* @return the number of strong components
"""
def count(self):
return self._count
"""
* Are vertices v and w in the same strong component?
* @param v one vertex
* @param w the other vertex
* @return true if vertices v and w are in the same
* strong component, and false otherwise
* @throws IllegalArgumentException unless 0 <= v < V
* @throws IllegalArgumentException unless 0 <= w < V
"""
def strongly_connected(self, v, w):
self._validate_vertex(v)
self._validate_vertex(w)
return self._id[v] == self._id[w]
"""
* Returns the component id of the strong component containing vertex v.
* @param v the vertex
* @return the component id of the strong component containing vertex v
* @throws IllegalArgumentException unless 0 <= s < V
"""
def id(self, v):
self._validate_vertex(v)
return self._id[v]
# does the id[] array contain the strongly connected components?
def _check(self, G):
tc = TransitiveClosure(G)
for v in range(G.V()):
for w in range(G.V()):
if self.strongly_connected(v, w) != (
tc.reachable(v, w) and tc.reachable(w, v)
):
return False
return True
# throw an IllegalArgumentException unless 0 <= v < V
def _validate_vertex(self, v):
V = len(self._marked)
if v < 0 or v >= V:
raise IllegalArgumentException(
"vertex {} is not between 0 and {}".format(v, V - 1)
)
def main(args):
stream = InStream(args[0])
G = Digraph.from_stream(stream)
scc = KosarajuSharirSCC(G)
# number of connected components
m = scc.count()
print("{} strong components".format(m))
# compute list of vertices in each strong component
components = [Queue() for i in range(m)]
for v in range(G.V()):
components[scc.id(v)].enqueue(v)
# print results
for i in range(m):
for v in components[i]:
print(str(v), end=" ")
print()
if __name__ == "__main__":
main(sys.argv[1:])
================================================
FILE: itu/algs4/graphs/kruskal_mst.py
================================================
import sys
from itu.algs4.fundamentals.queue import Queue
from itu.algs4.fundamentals.uf import WeightedQuickUnionUF
from itu.algs4.graphs.edge_weighted_graph import EdgeWeightedGraph
from itu.algs4.sorting.min_pq import MinPQ
from itu.algs4.stdlib.instream import InStream
# Created for BADS 2018
# See README.md for details
# Python 3
class KruskalMST:
"""The KruskalMST class represents a data type for computing a minimum
spanning tree in an edge-weighted graph.
The edge weights can be positive, zero, or negative and need not be
distinct. If the graph is not connected, it computes a minimum
spanning forest, which is the union of minimum spanning trees in
each connected component. The weight method returns the weight of a
minimum spanning tree and the edges method returns its edges. This
implementation uses Kruskal's algorithm and the union-find data
type. The constructor takes time proportional to E log E and extra
space (not including the graph) proportional to V, where V is the
number of vertices and E is the number of edges- Afterwards, the
weight method takes constant time and the edges method takes time
proportional to V.
"""
def __init__(self, G):
"""Computes a minimum spanning tree (or forest) of an edge-weighted
graph.
:param G: the edge-weighted graph
"""
self._weight = 0
self._mst = Queue()
pq = MinPQ()
for e in G.edges():
pq.insert(e)
uf = WeightedQuickUnionUF(G.V())
while not pq.is_empty() and self._mst.size() < G.V() - 1:
e = pq.del_min()
v = e.either()
w = e.other(v)
if not uf.connected(v, w):
uf.union(v, w)
self._mst.enqueue(e)
self._weight += e.weight()
def edges(self):
"""Returns the edges in a minimum spanning tree (or forest).
:return: the edges in a minimum spanning tree (or forest)
"""
return self._mst
def weight(self):
"""Returns the sum of the edge weights in a minimum spanning tree (or
forest).
:return: the sum of the edge weights in a minimum spanning tree (or forest)
"""
return self._weight
def main():
"""Creates an edge-weighted graph from an input file, runs Kruskal's
algorithm on it, and prints the edges of the MST and the sum of the edge
weights."""
if len(sys.argv) > 1:
stream = InStream(sys.argv[1])
G = EdgeWeightedGraph.from_stream(stream)
mst = KruskalMST(G)
for e in mst.edges():
print(e)
print("{:.5f}".format(mst.weight()))
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/graphs/lazy_prim_mst.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
from itu.algs4.fundamentals.queue import Queue
from itu.algs4.fundamentals.uf import UF
from itu.algs4.sorting.min_pq import MinPQ
class LazyPrimMST:
"""The LazyPrimMST class represents a data type for computing a minimum
spanning tree in an edge-weighted graph. The edge weights can be positive,
zero, or negative and need not be distinct. If the graph is not connected,
it computes a minimum spanning forest, which is the union of minimum
spanning trees in each connected component. The weight() method returns the
weight of a minimum spanning tree and the edges() method returns its edges.
This implementation uses a lazy version of Prim's algorithm with a
binary heap of edges. The constructor takes time proportional to E
log E and extra space (not including the graph) proportional to E,
where V is the number of vertices and E is the number of edges.
Afterwards, the weight() method takes constant time and the edges()
method takes time proportional to V.
"""
FLOATING_POINT_EPSILON = 1e-12
def __init__(self, G):
"""Compute a minimum spanning tree (or forest) of an edge-weighted
graph.
:param G: the edge-weighted graph
"""
self._weight = 0.0 # total weight of MST
self._mst = Queue() # edges in the MST
self._marked = [False] * G.V() # marked[v] = True if v on tree
self._pq = MinPQ() # edges with one endpoint in tree
for v in range(G.V()): # run Prim from all vertices to
if not self._marked[v]:
self._prim(G, v) # get a minimum spanning forest
# check optimality conditions
assert self._check(G)
def _prim(self, G, s):
# run Prim's algorithm
self._scan(G, s)
while not self._pq.is_empty(): # better to stop when mst has V-1 edges
e = self._pq.del_min() # smallest edge on pq
v = e.either() # two endpoints
w = e.other(v)
assert self._marked[v] or self._marked[w]
if (
self._marked[v] and self._marked[w]
): # lazy, both v and w already scanned
continue
self._mst.enqueue(e) # add e to MST
self._weight += e.weight()
if not self._marked[v]:
self._scan(G, v) # v becomes part of tree
if not self._marked[w]:
self._scan(G, w) # w becomes part of tree
def _scan(self, G, v):
# add all edges e incident to v onto pq if the other endpoint has not yet been scanned
assert not self._marked[v]
self._marked[v] = True
for e in G.adj(v):
if not self._marked[e.other(v)]:
self._pq.insert(e)
def edges(self):
"""Returns the edges in a minimum spanning tree (or forest).
:returns: the edges in a minimum spanning tree (or forest) as
an iterable of edges
"""
return self._mst
def weight(self):
"""Returns the sum of the edge weights in a minimum spanning tree (or
forest).
:returns: the sum of the edge weights in a minimum spanning tree (or forest)
"""
return self._weight
def _check(self, G):
# check optimality conditions (takes time proportional to E V lg* V)
totalWeight = 0.0 # check weight
for e in self.edges():
totalWeight += e.weight()
if abs(totalWeight - self.weight()) > LazyPrimMST.FLOATING_POINT_EPSILON:
error = "Weight of edges does not equal weight(): {} vs. {}\n".format(
totalWeight, self.weight()
)
print(error, file=sys.stderr)
return False
# check that it is acyclic
uf = UF(G.V())
for e in self.edges():
v = e.either()
w = e.other(v)
if uf.connected(v, w):
print("Not a forest", file=sys.stderr)
return False
uf.union(v, w)
# check that it is a spanning forest
for e in G.edges():
v = e.either()
w = e.other(v)
if not uf.connected(v, w):
print("Not a forest", file=sys.stderr)
return False
# check that it is a minimal spanning forest (cut optimality conditions)
for e in self.edges():
# all edges in MST except e
uf = UF(G.V())
for f in self._mst:
x = f.either()
y = f.other(x)
if f != e:
uf.union(x, y)
# check that e is min weight edge in crossing cut
for f in G.edges():
x = f.either()
y = f.other(x)
if not uf.connected(x, y):
if f.weight() < e.weight():
error = "Edge {} violates cut optimality conditions".format(f)
print(error, file=sys.stderr)
return False
return True
if __name__ == "__main__":
import sys
from itu.algs4.graphs.edge_weighted_graph import EdgeWeightedGraph
from itu.algs4.stdlib import stdio
from itu.algs4.stdlib.instream import InStream
In = InStream(sys.argv[1])
G = EdgeWeightedGraph.from_stream(In)
mst = LazyPrimMST(G)
for e in mst.edges():
stdio.writeln(e)
stdio.writef("%.5f\n", mst.weight())
================================================
FILE: itu/algs4/graphs/prim_mst.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
import math
import sys
from itu.algs4.fundamentals.queue import Queue
from itu.algs4.fundamentals.uf import UF
from itu.algs4.sorting.index_min_pq import IndexMinPQ
class PrimMST:
"""The PrimMST class represents a data type for computing a minimum
spanning tree in an edge-weighted graph. The edge weights can be positive,
zero, or negative and need not be distinct. If the graph is not connected,
it computes a minimum spanning forest, which is the union of minimum
spanning trees in each connected component. The weight() method returns the
weight of a minimum spanning tree and the edges() method returns its edges.
This implementation uses Prim's algorithm with an indexed binary
heap. The constructor takes time proportional to E log V and extra
space not including the graph) proportional to V, where V is the
number of vertices and E is the number of edges. Afterwards, the
weight() method takes constant time and the edges() method takes
time proportional to V.
"""
FLOATING_POINT_EPSILON = 1e-12
def __init__(self, G):
"""Compute a minimum spanning tree (or forest) of an edge-weighted
graph.
:param G: the edge-weighted graph
"""
self._edge_to = [
None
] * G.V() # self._edge_to[v] = shortest edge from tree vertex to non-tree vertex
self._dist_to = [0.0] * G.V() # self._dist_to[v] = weight of shortest such edge
self._marked = [
False
] * G.V() # self._marked[v] = True if v on tree, False otherwise
self._pq = IndexMinPQ(G.V())
for v in range(G.V()):
self._dist_to[v] = math.inf
for v in range(G.V()): # run from each vertex to find
if not self._marked[v]:
self._prim(G, v) # minimum spanning forest
# check optimality conditions
assert self._check(G)
# run Prim's algorithm in graph G, starting from vertex s
def _prim(self, G, s):
self._dist_to[s] = 0.0
self._pq.insert(s, self._dist_to[s])
while not self._pq.is_empty():
v = self._pq.del_min()
self._scan(G, v)
def _scan(self, G, v):
# scan vertex v
self._marked[v] = True
for e in G.adj(v):
w = e.other(v)
if self._marked[w]:
continue # v-w is obsolete edge
if e.weight() < self._dist_to[w]:
self._dist_to[w] = e.weight()
self._edge_to[w] = e
if self._pq.contains(w):
self._pq.decrease_key(w, self._dist_to[w])
else:
self._pq.insert(w, self._dist_to[w])
def edges(self):
"""Returns the edges in a minimum spanning tree (or forest).
:returns: the edges in a minimum spanning tree (or forest) as
an iterable of edges
"""
mst = Queue()
for v in range(len(self._edge_to)):
e = self._edge_to[v]
if e is not None:
mst.enqueue(e)
return mst
def weight(self):
"""Returns the sum of the edge weights in a minimum spanning tree (or
forest).
:returns: the sum of the edge weights in a minimum spanning tree (or forest)
"""
weight = 0.0
for e in self.edges():
weight += e.weight()
return weight
def _check(self, G):
# check optimality conditions (takes time proportional to E V lg* V)
totalWeight = 0.0 # check weight
for e in self.edges():
totalWeight += e.weight()
if abs(totalWeight - self.weight()) > PrimMST.FLOATING_POINT_EPSILON:
error = "Weight of edges does not equal weight(): {} vs. {}\n".format(
totalWeight, self.weight()
)
print(error, file=sys.stderr)
return False
# check that it is acyclic
uf = UF(G.V())
for e in self.edges():
v = e.either()
w = e.other(v)
if uf.connected(v, w):
print("Not a forest", file=sys.stderr)
return False
uf.union(v, w)
# check that it is a spanning forest
for e in G.edges():
v = e.either()
w = e.other(v)
if not uf.connected(v, w):
print("Not a spanning forest", file=sys.stderr)
return False
# check that it is a minimal spanning forest (cut optimality conditions)
for e in self.edges():
# all edges in MST except e
uf = UF(G.V())
for f in self.edges():
x = f.either()
y = f.other(x)
if f != e:
uf.union(x, y)
# check that e is min weight edge in crossing cut
for f in G.edges():
x = f.either()
y = f.other(x)
if not uf.connected(x, y):
if f.weight() < e.weight():
error = "Edge {} violates cut optimality conditions".format(f)
print(error, file=sys.stderr)
return False
return True
if __name__ == "__main__":
from itu.algs4.graphs.edge_weighted_graph import EdgeWeightedGraph
from itu.algs4.stdlib import stdio
from itu.algs4.stdlib.instream import InStream
In = InStream(sys.argv[1])
G = EdgeWeightedGraph.from_stream(In)
mst = PrimMST(G)
for e in mst.edges():
stdio.writeln(e)
stdio.writef("%.5f\n", mst.weight())
================================================
FILE: itu/algs4/graphs/symbol_digraph.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
from itu.algs4.graphs.digraph import Digraph
from itu.algs4.searching.binary_search_st import BinarySearchST
from itu.algs4.stdlib import stdio
from itu.algs4.stdlib.instream import InStream
class SymbolDigraph:
"""The SymbolDigraph class representsclass represents a digraph, where the
vertex names are arbitrary strings. By providing mappings between vertex
names and integers, it serves as a wrapper around the Digraph data type,
which assumes the.
vertex names are integers between 0 and V - 1.
It also supports initializing a symbol digraph from a file.
This implementation uses an ST to map from strings to integers,
an array to map from integers to strings, and a Digraph to store
the underlying graph.
The index_of and contains operations take time
proportional to log V, where V is the number of vertices.
The name_of operation takes constant time.
"""
def __init__(self, filename, delimiter):
"""Initializes a digraph from a file using the specified delimiter.
Each line in the file contains the name of a vertex, followed by a list
of the names of the vertices adjacent to that vertex, separated by the
delimiter.
:param filename: the name of the file
:param delimiter: the delimiter between fields
"""
self._st = BinarySearchST() # string -> index
# First pass builds the index by reading strings to associate
# distinct strings with an index
stream = InStream(filename)
while not stream.isEmpty():
a = stream.readLine().split(delimiter)
for i in range(len(a)):
if not self._st.contains(a[i]):
self._st.put(a[i], self._st.size())
stdio.writef("Done reading %s\n", filename)
# inverted index to get keys in an array
self._keys = [None] * self._st.size() # index -> string
for name in self._st.keys():
self._keys[self._st.get(name)] = name
# second pass builds the graph by connecting first vertex on each
# line to all others
self._graph = Digraph(self._st.size()) # the underlying graph
stream = InStream(filename)
while stream.hasNextLine():
a = stream.readLine().split(delimiter)
v = self._st.get(a[0])
for i in range(1, len(a)):
w = self._st.get(a[i])
self._graph.add_edge(v, w)
def contains(self, s):
"""Does the graph contain the vertex named s?
:param s: the name of a vertex
:return:s true if s is the name of a vertex, and false otherwise
"""
return self._st.contains(s)
def index_of(self, s):
"""Returns the integer associated with the vertex named s.
:param s: the name of a vertex
:returns: the integer (between 0 and V - 1) associated with the vertex named s
"""
return self._st.get(s)
def name_of(self, v):
"""Returns the name of the vertex associated with the integer v.
@param v the integer corresponding to a vertex (between 0 and V - 1)
@throws IllegalArgumentException unless 0 <= v < V
@return the name of the vertex associated with the integer v
"""
self._validateVertex(v)
return self._keys[v]
def digraph(self):
return self._graph
def _validateVertex(self, v):
# throw an IllegalArgumentException unless 0 <= v < V
V = self._graph.V()
if v < 0 or v >= V:
raise ValueError("vertex {} is not between 0 and {}".format(v, V - 1))
if __name__ == "__main__":
import sys
filename = sys.argv[1]
delimiter = sys.argv[2]
sg = SymbolDigraph(filename, delimiter)
graph = sg.digraph()
while stdio.hasNextLine():
source = stdio.readLine()
if sg.contains(source):
s = sg.index_of(source)
for v in graph.adj(s):
stdio.writef("\t%s\n", sg.name_of(v))
else:
stdio.writef("input not contain '%i'", source)
================================================
FILE: itu/algs4/graphs/symbol_graph.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
from itu.algs4.graphs.graph import Graph
from itu.algs4.searching.binary_search_st import BinarySearchST
from itu.algs4.stdlib import stdio
from itu.algs4.stdlib.instream import InStream
class SymbolGraph:
"""The SymbolGraph class represents an undirected graph, where the vertex
names are arbitrary strings. By providing mappings between vertex names and
integers, it serves as a wrapper around the Graph data type, which assumes
the vertex names are integers.
between 0 and V - 1.
It also supports initializing a symbol graph from a file.
This implementation uses an ST to map from strings to integers,
an array to map from integers to strings, and a Graph to store
the underlying graph.
The index_of and contains operations take time
proportional to log V, where V is the number of vertices.
The name_of operation takes constant time.
"""
def __init__(self, filename, delimiter):
"""Initializes a graph from a file using the specified delimiter. Each
line in the file contains the name of a vertex, followed by a list of
the names of the vertices adjacent to that vertex, separated by the
delimiter.
:param filename: the name of the file
:param delimiter: the delimiter between fields
"""
self._st = BinarySearchST() # string -> index
# First pass builds the index by reading strings to associate
# distinct strings with an index
stream = InStream(filename)
while not stream.isEmpty():
a = stream.readLine().split(delimiter)
for i in range(len(a)):
if not self._st.contains(a[i]):
self._st.put(a[i], self._st.size())
stdio.writef("Done reading %s\n", filename)
# inverted index to get keys in an array
self._keys = [None] * self._st.size() # index -> string
for name in self._st.keys():
self._keys[self._st.get(name)] = name
# second pass builds the graph by connecting first vertex on each
# line to all others
self._graph = Graph(self._st.size()) # the underlying graph
stream = InStream(filename)
while stream.hasNextLine():
a = stream.readLine().split(delimiter)
v = self._st.get(a[0])
for i in range(1, len(a)):
w = self._st.get(a[i])
self._graph.add_edge(v, w)
def contains(self, s):
"""Does the graph contain the vertex named s?
:param s: the name of a vertex
:return:s true if s is the name of a vertex, and false otherwise
"""
return self._st.contains(s)
def index_of(self, s):
"""Returns the integer associated with the vertex named s.
:param s: the name of a vertex
:returns: the integer (between 0 and V - 1) associated with the vertex named s
"""
return self._st.get(s)
def name_of(self, v):
"""Returns the name of the vertex associated with the integer v.
@param v the integer corresponding to a vertex (between 0 and V - 1)
@throws IllegalArgumentException unless 0 <= v < V
@return the name of the vertex associated with the integer v
"""
self._validateVertex(v)
return self._keys[v]
def graph(self):
return self._graph
def _validateVertex(self, v):
# throw an IllegalArgumentException unless 0 <= v < V
V = self._graph.V()
if v < 0 or v >= V:
raise ValueError("vertex {} is not between 0 and {}".format(v, V - 1))
if __name__ == "__main__":
import sys
filename = sys.argv[1]
delimiter = sys.argv[2]
sg = SymbolGraph(filename, delimiter)
graph = sg.graph()
while stdio.hasNextLine():
source = stdio.readLine()
if sg.contains(source):
s = sg.index_of(source)
for v in graph.adj(s):
stdio.writef("\t%s\n", sg.name_of(v))
else:
stdio.writef("input not contain '%i'", source)
================================================
FILE: itu/algs4/graphs/topological.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
"""This module implements the topological order algorithm described in
Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
For more information, see chapter 4.2 of the book.
"""
from itu.algs4.graphs.depth_first_order import DepthFirstOrder
from itu.algs4.graphs.digraph import Digraph
from itu.algs4.graphs.directed_cycle import DirectedCycle
from itu.algs4.graphs.edge_weighted_directed_cycle import EdgeWeightedDirectedCycle
class Topological:
"""The Topological class represents a data type for determining a
topological order of a directed acyclic graph (DAG). Recall, a digraph has
a topological order if and only if it is a DAG. The hasOrder operation
determines whether the digraph has a topological order, and if so, the
order operation returns one.
This implementation uses depth-first search. The constructor takes time
proportional to V + E (in the worst case), where V is the number of vertices
and E is the number of edges. Afterwards, the hasOrder and rank operations
takes constant time the order operation takes time proportional to V.
See DirectedCycle, DirectedCycleX, and EdgeWeightedDirectedCycle to compute
a directed cycle if the digraph is not a DAG. See TopologicalX for a
nonrecursive queue-based algorithm to compute a topological order of a DAG.
For additional documentation, see Section 4.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
"""
def __init__(self, digraph):
"""Determines whether the digraph (or edge weighted digraph) has a
topological order and, if so, finds such a topological order.
:param digraph: the Digraph or EdgeWeightedDigraph to check
"""
self._order = None
if isinstance(digraph, Digraph):
finder = DirectedCycle(digraph)
else:
finder = EdgeWeightedDirectedCycle(digraph)
if not finder.has_cycle():
dfs = DepthFirstOrder(digraph)
self._order = dfs.reverse_post()
self._rank = [0] * digraph.V()
i = 0
for v in self._order:
self._rank[v] = i
i += 1
def order(self):
"""Returns a topological order if the digraph has a topologial order,
and None otherwise.
:returns: a topological order of the vertices (as an interable) if the digraph has a
topological order (or equivalently, if the digraph is a DAG), and None otherwise
"""
return self._order
def has_order(self):
"""Does the digraph have a topological order?
:returns: True if the digraph has a topological order (or equivalently, if the digraph
is a DAG), and False otherwise
"""
return self._order is not None
def rank(self, v):
"""The the rank of vertex v in the topological order -1 if the digraph
is not a DAG.
:param v: the vertex
:returns: the position of vertex v in a topological order of the digraph -1 if the digraph is not a DAG
"""
self._validate_vertex(v)
if self.has_order():
return self._rank[v]
else:
return -1
def _validate_vertex(self, v):
# throw an IllegalArgumentException unless 0 <= v < V
V = len(self._rank)
if v < 0 or v >= V:
raise ValueError("vertex {} is not between 0 and {}", v, (V - 1))
if __name__ == "__main__":
import sys
from itu.algs4.graphs.symbol_digraph import SymbolDigraph
from itu.algs4.stdlib import stdio
filename = sys.argv[1]
delimiter = sys.argv[2]
sg = SymbolDigraph(filename, delimiter)
topological = Topological(sg.digraph())
for v in topological.order():
stdio.writeln(sg.name_of(v))
================================================
FILE: itu/algs4/graphs/transitive_closure.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
"""
* Execution: python transitive_closure.py filename.txt
* Dependencies: Digraph DirectedDFS
* Data files: https:#algs4.cs.princeton.edu/42digraph/tinyDG.txt
*
* Compute transitive closure of a digraph and support
* reachability queries.
*
* Preprocessing time: O(V(E + V)) time.
* Query time: O(1).
* Space: O(V^2).
*
* % python transitive_closure.py tinyDG.txt
* 0 1 2 3 4 5 6 7 8 9 10 11 12
* --------------------------------------------
* 0: T T T T T T
* 1: T
* 2: T T T T T T
* 3: T T T T T T
* 4: T T T T T T
* 5: T T T T T T
* 6: T T T T T T T T T T T
* 7: T T T T T T T T T T T T T
* 8: T T T T T T T T T T T T T
* 9: T T T T T T T T T T
* 10: T T T T T T T T T T
* 11: T T T T T T T T T T
* 12: T T T T T T T T T T
*
"""
import sys
from itu.algs4.errors.errors import IllegalArgumentException
from itu.algs4.graphs.digraph import Digraph
from itu.algs4.graphs.directed_dfs import DirectedDFS
from itu.algs4.stdlib.instream import InStream
class TransitiveClosure:
"""
* Computes the transitive closure of the digraph G.
* @param G the digraph
"""
def __init__(self, G):
self._tc = [None] * G.V() # tc[v] = reachable from v
for v in range(G.V()):
self._tc[v] = DirectedDFS(G, v)
"""
* Is there a directed path from vertex v to vertex w in the digraph?
* @param v the source vertex
* @param w the target vertex
* @return true if there is a directed path from v to w,
* false otherwise
* @throws IllegalArgumentException unless 0 <= v < V
* @throws IllegalArgumentException unless 0 <= w < V
"""
def reachable(self, v, w):
self._validate_vertex(v)
self._validate_vertex(w)
return self._tc[v].is_marked(w)
# throw an IllegalArgumentException unless 0 <= v < V
def _validate_vertex(self, v):
V = len(self._tc)
if v < 0 or v >= V:
raise IllegalArgumentException(
"vertex {} is not between 0 and {}".format(v, V - 1)
)
def main(args):
stream = InStream(args[0])
G = Digraph.from_stream(stream)
tc = TransitiveClosure(G)
# print header
print(" ", end="")
for v in range(G.V()):
print("{x:3d}".format(x=v), end="")
print()
print("--------------------------------------------")
# print transitive closure
for v in range(G.V()):
print("{x:3d}: ".format(x=v), end="")
for w in range(G.V()):
if tc.reachable(v, w):
print(" T", end="")
else:
print(" ", end="")
print()
if __name__ == "__main__":
main(sys.argv[1:])
================================================
FILE: itu/algs4/searching/__init__.py
================================================
================================================
FILE: itu/algs4/searching/binary_search_st.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
from itu.algs4.fundamentals.queue import Queue
class BinarySearchST:
"""The BST class represents an ordered symbol table of generic key-value
pairs. It supports the usual put, get, contains, delete, size, and is-empty
methods. It also provides ordered methods for finding the minimum, maximum,
floor, select, and ceiling. It also provides a keys method for iterating
over all of the keys. A symbol table implements the associative array
abstraction: when associating a value with a key that is already in the
symbol table, the convention is to replace the old value with the new
value. Unlike java.util.Map, this class uses the convention that values
cannot be None—setting the value associated with a key to None is
equivalent to deleting the key from the symbol table.
This implementation uses a sorted array. It requires that the key
type implements the Comparable interface and calls the compareTo()
and method to compare two keys. It does not call either equals() or
hashCode(). The put and remove operations each take linear time in
the worst case the contains, ceiling, floor, and rank operations
take logarithmic time the size, is-empty, minimum, maximum, and
select operations take constant time. Construction takes constant
time.
"""
_INIT_CAPACITY = 2
def __init__(self, capacity=_INIT_CAPACITY):
"""Initializes an empty symbol table with the specified initial
capacity.
:param capacity: the maximum capacity
"""
self._keys = [None] * capacity
self._vals = [None] * capacity
self._n = 0
def _resize(self, capacity):
# resize the underlying "arrays"
assert capacity >= self._n
tempk = [None] * capacity
tempv = [None] * capacity
for i in range(self._n):
tempk[i] = self._keys[i]
tempv[i] = self._vals[i]
self._vals = tempv
self._keys = tempk
def size(self):
"""Returns the number of key-value pairs in this symbol table.
:returns: the number of key-value pairs in this symbol table
"""
return self._n
def __len__(self):
return self.size()
def is_empty(self):
"""Returns True if this symbol table is empty.
:returns: True if this symbol table is empty
False otherwise
"""
return self.size() == 0
def contains(self, key):
"""Does this symbol table contain the given key?
:param key: the key
:returns: True if this symbol table contains key and
False otherwise
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to contains() is None")
return self.get(key) is not None
def get(self, key):
"""Returns the value associated with the given key in this symbol
table.
:param key: the key
:returns: the value associated with the given key if the key is in the symbol table
and None if the key is not in the symbol table
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to get() is None")
if self.is_empty():
return None
i = self.rank(key)
if i < self._n and self._keys[i] == key:
return self._vals[i]
return None
def rank(self, key):
"""Returns the number of keys in this symbol table strictly less than
key.
:param key: the key
:returns: the number of keys in the symbol table strictly less than key
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to rank() is None")
lo = 0
hi = self._n - 1
while lo <= hi:
mid = int(lo + (hi - lo) / 2)
if key < self._keys[mid]:
hi = mid - 1
elif key > self._keys[mid]:
lo = mid + 1
else:
return mid
return lo
def put(self, key, val):
"""Inserts the specified key-value pair into the symbol table,
overwriting the old value with the new value if the symbol table
already contains the specified key. Deletes the specified key (and its
associated value) from this symbol table if the specified value is
None.
:param key: the key
:param val: the value
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("first argument to put() is None")
if val is None:
self.delete(key)
return
i = self.rank(key)
# key is already in table
if i < self._n and self._keys[i] == key:
self._vals[i] = val
return
# insert new key-value pair
if self._n == len(self._keys):
self._resize(2 * len(self._keys))
j = self._n
while j > i:
self._keys[j] = self._keys[j - 1]
self._vals[j] = self._vals[j - 1]
j -= 1
self._keys[i] = key
self._vals[i] = val
self._n += 1
assert self._check()
def delete(self, key):
"""Removes the specified key and associated value from this symbol
table (if the key is in the symbol table).
:param key: the key
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to delete() is None")
if self.is_empty():
return
# compute rank
i = self.rank(key)
n = self._n
# key not in table
if i == n or self._keys[i] != key:
return
j = i
while j < self._n - 1:
self._keys[j] = self._keys[j + 1]
self._vals[j] = self._vals[j + 1]
j += 1
self._n -= 1
n = self._n
self._keys[n] = None # to avoid loitering
self._vals[n] = None
# resize if 1/4 full
if n > 0 and n == len(self._keys) // 4:
self._resize(len(self._keys) // 2)
assert self._check()
def deleteMin(self):
"""Removes the smallest key and associated value from this symbol
table.
:raises ValueError: if the symbol table is empty
"""
if self.is_empty():
raise ValueError("Symbol table underflow error")
self.delete(self.min())
def deleteMax(self):
"""Removes the largest key and associated value from this symbol table.
:raises ValueError: if the symbol table is empty
"""
if self.is_empty():
raise ValueError("Symbol table underflow error")
self.delete(self.max())
# *************************************************************************
# Ordered symbol table methods.
# *************************************************************************
def min(self):
"""Returns the smallest key in this symbol table.
:returns: the smallest key in this symbol table
:raises ValueError: if this symbol table is empty
"""
if self.is_empty():
raise ValueError("called min() with empty symbol table")
return self._keys[0]
def max(self):
"""Returns the largest key in this symbol table.
:returns: the largest key in this symbol table
:raises ValueError: if this symbol table is empty
"""
if self.is_empty():
raise ValueError("called max() with empty symbol table")
return self._keys[self._n - 1]
def select(self, k):
"""Return the kth smallest key in this symbol table.
:param k: the order statistic
:returns: the kth smallest key in this symbol table
:raises ValueError: unless k is between 0 and n-1
"""
if k < 0 or k >= self.size():
raise ValueError("called select() with invalid argument: {}".format(k))
return self._keys[k]
def floor(self, key):
"""Returns the largest key in this symbol table less than or equal to
key.
:param key: the key
:returns: the largest key in this symbol table less than or equal to key
:raises ValueError: if there is no such key
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to floor() is None")
i = self.rank(key)
if i < self._n and key == self._keys[i]:
return self._keys[i]
if i == 0:
return None
else:
return self._keys[i - 1]
def ceiling(self, key):
"""Returns the smallest key in this symbol table greater than or equal
to key.
:param key: the key
:returns: the smallest key in this symbol table greater than or equal to key
:raises ValueError: if there is no such key
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to ceiling() is None")
i = self.rank(key)
if i == self._n:
return None
else:
return self._keys[i]
def size_between(self, lo, hi):
"""Returns the number of keys in this symbol table in the specified
range.
:param lo: minimum endpoint
:param hi: maximum endpoint
:returns: the number of keys in this symbol table between lo
(inclusive) and hi (inclusive)
:raises ValueError: if either lo or hi is None
"""
if lo is None:
raise ValueError("first argument to size() is None")
if hi is None:
raise ValueError("second argument to size() is None")
if lo > hi:
return 0
if self.contains(hi):
return self.rank(hi) - self.rank(lo) + 1
else:
return self.rank(hi) - self.rank(lo)
def keys(self):
"""
Returns all keys in this symbol table as an Iterable.
To iterate over all of the keys in the symbol table named st,
use the foreach notation: for (Key key : st.keys()).
:returns: all keys in this symbol table
"""
return self.keys_between(self.min(), self.max())
def keys_between(self, lo, hi):
"""Returns all keys in this symbol table in the given range, as an
Iterable.
:param lo: minimum endpoint
:param hi: maximum endpoint
:returns: all keys in this symbol table between lo
(inclusive) and hi (inclusive)
:raises ValueError: if either lo or hi are None
"""
if lo is None:
raise ValueError("first argument to keys() is None")
if hi is None:
raise ValueError("second argument to keys() is None")
queue = Queue()
if lo > hi:
return queue
i = self.rank(lo)
end = self.rank(hi)
while i < end:
queue.enqueue(self._keys[i])
i += 1
if self.contains(hi):
queue.enqueue(self._keys[self.rank(hi)])
return queue
# *************************************************************************
# Check internal invariants.
# *************************************************************************
def _check(self):
return self._is_sorted() and self._rank_check()
def _is_sorted(self):
# are the items in the array in ascending order?
i = 1
while i < self.size():
if self._keys[i] < self._keys[i - 1]:
return False
i += 1
return True
def _rank_check(self):
# check that rank(select(i)) = i
for i in range(self.size()):
if i != self.rank(self.select(i)):
return False
for i in range(self.size()):
if self._keys[i] != self.select(self.rank(self._keys[i])):
return False
return True
if __name__ == "__main__":
from itu.algs4.stdlib import stdio
st = BinarySearchST()
i = 0
while not stdio.isEmpty():
key = stdio.readString()
st.put(key, i)
i += 1
for s in st.keys():
stdio.writef("%s %i\n", s, st.get(s))
================================================
FILE: itu/algs4/searching/bst.py
================================================
# Created for BADS 2018
# see README.md for details
# Python 3
import sys
from abc import abstractmethod
from typing import Generic, Optional, TypeVar
from typing_extensions import Protocol
from ..errors.errors import IllegalArgumentException, NoSuchElementException
from ..fundamentals.queue import Queue
sys.setrecursionlimit(10 ** 5)
"""
The BST class represents an ordered symbol table of generic
key-value pairs.
This implementation uses an unbalanced, binary search tree.
For additional details and documentation, see Section 3.2 of Algorithms,
4th Edition by Robert Sedgewick and Kevin Wayne.
:original author: Robert Sedgewick and Kevin Wayne
:original java code: https://algs4.cs.princeton.edu/32bst/BST.java.html
"""
Val = TypeVar("Val")
Key = TypeVar("Key", bound="Comparable")
class Comparable(Protocol):
@abstractmethod
def __lt__(self: Key, other: Key) -> bool:
pass
class Node(Generic[Key, Val]):
def __init__(self, key: Key, value: Optional[Val], size: int):
self.left: Optional[Node[Key, Val]] = None # root of left subtree
self.right: Optional[Node[Key, Val]] = None # root of right subtree
self.key: Key = key # sorted by key
self.value: Optional[Val] = value # associated data
self.size: int = size # number of nodes in subtree
class BST(Generic[Key, Val]):
def __init__(self) -> None:
"""Initialises empty symbol table."""
self._root: Optional[Node[Key, Val]] = None # root of BST
def is_empty(self) -> bool:
"""Returns true if this symbol table is empty."""
return self.size() == 0
def contains(self, key: Key) -> bool:
"""Does this symbol table contain the given key?
:param key: the key to search for
:return boolean: true if symbol table contains key, false otherwise
"""
return self.get(key) is not None
def size(self) -> int:
"""Returns the number of key-value pairs in this symbol table."""
return self._size(self._root)
def __len__(self) -> int:
return self.size()
def _size(self, node: Optional[Node[Key, Val]]) -> int:
"""Returns the number of key-value pairs in BST rooted at node.
:param node: The node which act as root
"""
if node is None:
return 0
else:
return node.size
def get(self, key: Key) -> Optional[Val]:
"""Returns the value associated with the given key.
:param key: The key whose value is returned
:return: the value associated with the given key if the key
is in the symbol table, None otherwise
"""
return self._get(self._root, key)
def _get(self, node: Optional[Node[Key, Val]], key: Key) -> Optional[Val]:
if node is None:
return None
else:
if key < node.key:
return self._get(node.left, key)
elif key > node.key:
return self._get(node.right, key)
else:
return node.value
def put(self, key: Key, value: Optional[Val]) -> None:
"""Inserts the specified key-value pair into the symbol table,
overwriting the old value with the new value if the symbol table
already contains the specified key. Deletes the specified key (and its
associated value) from this symbol table if the specified value is
None.
:param key, value: the key-value pair to be inserted
"""
if value is None:
self.delete(key)
return
self._root = self._put(self._root, key, value)
def _put(
self, node: Optional[Node[Key, Val]], key: Key, value: Optional[Val]
) -> Node[Key, Val]:
if node is None:
newnode: Node[Key, Val] = Node(key, value, 1)
return newnode
else:
if key < (node.key):
node.left = self._put(node.left, key, value)
elif key > (node.key):
node.right = self._put(node.right, key, value)
else:
node.value = value
node.size = 1 + self._size(node.left) + self._size(node.right)
return node
def delete_min(self) -> None:
"""Removes the smallest key and associated value from the symbol table
TODO exception?"""
if self.is_empty():
raise NoSuchElementException("calls min() with empty symbol table")
else:
assert self._root is not None
self._root = self._delete_min(self._root)
def _delete_min(self, node: Node[Key, Val]) -> Optional[Node[Key, Val]]:
if node.left is None:
return node.right
else:
node.left = self._delete_min(node.left)
node.size = self._size(node.left) + self._size(node.right) + 1
return node
def delete_max(self) -> None:
"""Removes the largest key and associated value from the symbol
table."""
if self.is_empty():
raise NoSuchElementException("calls max() with empty symbol table")
else:
assert self._root is not None
self._root = self._delete_max(self._root)
def _delete_max(self, node: Node[Key, Val]) -> Optional[Node[Key, Val]]:
if node.right is None:
return node.left
else:
node.right = self._delete_max(node.right)
node.size = self._size(node.left) + self._size(node.right) + 1
return node
def delete(self, key: Key) -> None:
"""Removes the specified key and its associated value from this symbol
table (if the key is in this symbol table)"""
self._root = self._delete(self._root, key)
def _delete(
self, node: Optional[Node[Key, Val]], key: Key
) -> Optional[Node[Key, Val]]:
if node is None:
return None
else:
if key.__lt__(node.key):
node.left = self._delete(node.left, key)
elif node.key < key:
node.right = self._delete(node.right, key)
else:
if node.right is None:
return node.left
elif node.left is None:
return node.right
else:
temp_node = node
assert temp_node.right is not None
node = self._min(temp_node.right)
node.right = self._delete_min(temp_node.right)
node.left = temp_node.left
node.size = self._size(node.left) + self._size(node.right) + 1
return node
def min(self) -> Key:
"""Returns the smallest key in the BST."""
if self.is_empty():
raise NoSuchElementException("calls min() with empty symbol table")
else:
assert self._root is not None
return self._min(self._root).key
def _min(self, node: Node[Key, Val]) -> Node[Key, Val]:
if node.left is None:
return node
else:
return self._min(node.left)
def max(self) -> Key:
"""Returns the larget key in the symbol table."""
if self.is_empty():
raise NoSuchElementException("calls max() with empty symbol table")
else:
assert self._root is not None
return self._max(self._root).key
def _max(self, node: Node[Key, Val]) -> Node[Key, Val]:
if node.right is None:
return node
else:
return self._max(node.right)
def floor(self, key: Key) -> Key:
"""Returns the largest key in the symbol table less than or equal to
key Raises NoSuchElementException if no such key exists."""
if self.is_empty():
raise NoSuchElementException("calls floor() with empty symbol table")
node = self._floor(self._root, key)
if node is None:
raise NoSuchElementException("calls floor() with key < min")
else:
return node.key
def _floor(
self, node: Optional[Node[Key, Val]], key: Key
) -> Optional[Node[Key, Val]]:
if node is None:
return None
elif key == node.key:
return node
elif key < node.key:
return self._floor(node.left, key)
temp_node = self._floor(node.right, key)
if temp_node is not None:
return temp_node
return node
def ceiling(self, key: Key) -> Key:
"""Returns the smallest key in the symbol table greater than or equal
to key Raises NoSuchElementException if no such key exists."""
if self.is_empty():
raise NoSuchElementException("calls ceiling() with empty symbol table")
node = self._ceiling(self._root, key)
if node is None:
raise NoSuchElementException("calls ceiling() with key > max")
else:
return node.key
def _ceiling(
self, node: Optional[Node[Key, Val]], key: Key
) -> Optional[Node[Key, Val]]:
if node is None:
return None
elif key == node.key:
return node
elif key > node.key:
return self._ceiling(node.right, key)
temp_node = self._ceiling(node.left, key)
if temp_node is not None:
return temp_node
return node
def keys(self) -> Queue[Key]:
"""Returns all keys in the symbol table as a list."""
if self.is_empty():
return Queue()
return self.range_keys(self.min(), self.max())
def range_keys(self, lo: Key, hi: Key) -> Queue[Key]:
"""returns all keys in the symbol table in the given range as a list.
:param lo: minimum endpoint
:param hi: maximum endpoint
:return: all keys in symbol table between lo (inclusive) and hi (inclusive)
"""
queue: Queue[Key] = Queue()
self._range_keys(self._root, queue, lo, hi)
return queue
def _range_keys(
self, node: Optional[Node[Key, Val]], queue: Queue[Key], lo: Key, hi: Key
) -> None:
if node is None:
return
elif lo < node.key:
self._range_keys(node.left, queue, lo, hi)
if not lo > node.key and not hi < node.key:
queue.enqueue(node.key)
if hi > node.key:
self._range_keys(node.right, queue, lo, hi)
def select(self, k: int) -> Key:
"""Return the kth smallest key in the symbol table.
:param k: the order statistic
:return: the kth smallest key in the symbol table
:raises IllegalArgumentException: unless k is between 0 and n-1
"""
if k < 0 or k >= self.size():
raise IllegalArgumentException(
"argument to select() is invalid: {}".format(k)
)
assert self._root is not None
x = self._select(self._root, k)
return x.key
def _select(self, x: Node[Key, Val], k: int) -> Node[Key, Val]:
"""
Returns the node with key of rank k in the subtree rooted at x
:rtype: Node
"""
t = self._size(x.left)
if t > k:
assert x.left is not None
return self._select(x.left, k)
elif t < k:
assert x.right is not None
return self._select(x.right, k - t - 1)
else:
return x
def rank(self, key: Key) -> int:
"""Returns the number of keys in the symbol table strictly less than
key.
:param key: the key
:return: the number of keys in the symbol table strictly less than key
:rtype: int
:raises IllegalArgumentException: if key is None
"""
if key is None:
raise IllegalArgumentException("argument to rank() is None")
return self._rank(key, self._root)
def _rank(self, key: Key, x: Optional[Node[Key, Val]]) -> int:
"""Returns the number of keys less than key in the subtree rooted at x.
:rtype: int
"""
if x is None:
return 0
if key < x.key:
return self._rank(key, x.left)
if key > x.key:
return 1 + self._size(x.left) + self._rank(key, x.right)
else:
return self._size(x.left)
def size_range(self, lo: Key, hi: Key) -> int:
"""Returns the number of keys in the symbol table in the given range.
:param lo: minimum endpoint
:param hi: maximum endpoint
:return: the number of keys in the symbol table between lo
(inclusive) and hi (inclusive)
:rtype: int
:raises IllegalArgumentException: if either lo or hi is None
"""
if lo is None:
return IllegalArgumentException("first argument to size() is None")
if hi is None:
return IllegalArgumentException("second argument to size() is None")
if lo > hi:
return 0
if self.contains(hi):
return self.rank(hi) - self.rank(lo) + 1
else:
return self.rank(hi) - self.rank(lo)
def height(self) -> int:
"""Returns the height of the BST (for debugging)"""
return self._height(self._root)
def _height(self, node: Optional[Node[Key, Val]]) -> int:
if node is None:
return -1
else:
return 1 + max(self._height(node.left), self._height(node.right))
def level_order(self) -> Queue[Key]:
"""Returns the keys in the BST in level order (for debugging)"""
queue: Queue[Optional[Node[Key, Val]]] = Queue()
keys: Queue[Key] = Queue()
queue.enqueue(self._root)
while len(queue) > 0:
node = queue.dequeue()
if node is None:
continue
else:
keys.enqueue(node.key)
queue.enqueue(node.left)
queue.enqueue(node.right)
return keys
================================================
FILE: itu/algs4/searching/file_index.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
import sys
from itu.algs4.stdlib import stdio
# Execution: python file_index.py file1.txt file2.txt file3.txt ...
#
# % python file_index.py ex*.txt
# age
# ex3.txt
# ex4.txt
# best
# ex1.txt
# was
# ex1.txt
# ex2.txt
# ex3.txt
# ex4.txt
#
# % python file_index.py *.txt
#
# % python file_index.py *.py
"""
* The {@code FileIndex} class provides a client for indexing a set of files,
* specified as command-line arguments. It takes queries from standard input
* and prints each file that contains the given query.
"""
# key = word, value = set of files containing that word
if __name__ == "__main__":
st = {}
args = sys.argv[1:]
# create inverted index of all files
print("Indexing files")
for filename in args:
print(" " + filename)
file = open(filename, "r")
for line in file.readlines():
for word in line.split():
if word not in st:
st[word] = set()
s = st.get(word)
s.add(file)
# read queries from standard input, one per line
while not stdio.isEmpty():
query = stdio.readString()
if query in st:
s = st.get(query)
for file in s:
print(" " + file.name)
================================================
FILE: itu/algs4/searching/frequency_counter.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
import sys
from itu.algs4.stdlib import stdio
# Execution: python frequency_counter.py L < input.txt
#
# Read in a list of words from standard input and print out
# the most frequently occurring word that has length greater than
# a given threshold.
#
# % python frequency_counter.py 1 < tinyTale.txt
# it 10
#
# % python frequency_counter.py 8 < tale.txt
# business 122
#
# % python frequency_counter.py 10 < leipzig1M.txt
# government 24763
"""
The FrequencyCounter class provides a client for
reading in a sequence of words and printing a word (exceeding
a given length) that occurs most frequently. It is useful as
a test client for various symbol table implementations.
Reads in a command-line integer and sequence of words from
standard input and prints out a word (whose length exceeds
the threshold) that occurs most frequently to standard output.
It also prints out the number of words whose length exceeds
the threshold and the number of distinct such words.
"""
if __name__ == "__main__":
args = sys.argv[1:]
distinct = 0
words = 0
minlen = int(args[0])
st = {}
# compute frequency counts
while not stdio.isEmpty():
key = stdio.readString()
if len(key) < minlen:
continue
words += 1
if key in st:
st[key] = st.get(key) + 1
else:
st[key] = 1
distinct += 1
# find a key with the highest frequency count
max = ""
st[max] = 0
for word in st.keys():
if st.get(word) > st.get(max):
max = word
print(max + " " + str(st.get(max)))
print("distinct = " + str(distinct))
print("words = " + str(words))
================================================
FILE: itu/algs4/searching/linear_probing_hst.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
import sys
class LinearProbingHashST:
"""The LinearProbingHashST class represents a symbol table of dynamic key-
value pairs. It supports the usual put, get, contains, delete, size, and is-
empty methods. It also provides a key_list method for iterating over all of
the keys. A symbol table implements the associative array abstraction: when
associating a value with a key that is already in the symbol table, the
convention is to replace the old value with the new value. Unlike the Map-
class in Java, this class uses the convention that values cannot be null/None.
Setting the value associated with a key to None is equivalent to deleting the
key from the symbol table.
This implementation uses a linear probing hash table. It requires that
the key type overrides the __eq__ and __hash__ methods. The expected
time per put, contains, or remove operation is constant, subject to the
uniform hashing assumption. The size, and is-empty operations take
constant time. Construction takes constant time.
"""
def __init__(self, capacity=4):
"""Initializes an empty symbol table with the specified initial capacity. If
no capacity is specified, it defaults to 4.
:param capacity: the initial capacity
"""
self.m = capacity
self.n = 0
self.keys = [None for i in range(0, self.m)]
self.values = [None for i in range(0, self.m)]
def size(self):
"""Returns the number of key-value pairs in this symbol table.
:returns: the number of key-value pairs in this symbol table.
"""
return self.n
def __len__(self):
return self.size()
def is_empty(self):
"""Returns True if this symbol table is empty.
:returns: True if this symbol table is empty;
False otherwise
"""
return self.n == 0
def contains(self, key):
"""Returns True if this symbol table contains the specified key.
:param key: the key
:returns: True if this symbol table contains the key;
False otherwize
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to contains() is None")
return self.get(key) is not None
def _hash(self, key):
# Hash value between 0 and M-1
return (hash(key) & 0x7FFFFFFF) % self.m
def _resize(self, capacity):
# Resizes the hash table to the given capacity by re-hashing all of the keys
temp = LinearProbingHashST(capacity)
for i in range(0, self.m):
if self.keys[i] is not None:
temp.put(self.keys[i], self.values[i])
self.keys = temp.keys
self.values = temp.values
self.m = temp.m
def put(self, key, value):
"""Inserts the specified key-value paur into the symbol table, overwriting
the old value with the new value if the symbol table already contains the
specified key. Deletes the specified key (and its associated value) from this
symbol table if the specified value is None.
:param key: the key
:param value: the value
:raises ValueError: if key is None.
"""
if key is None:
raise ValueError("argument to put() is None")
if value is None:
self.delete(key)
return
# Double table size if 50% full
if self.n >= self.m // 2:
self._resize(2 * self.m)
i = self._hash(key)
while self.keys[i] is not None:
if self.keys[i] == key:
self.values[i] = value
return
i = (i + 1) % self.m
self.keys[i] = key
self.values[i] = value
self.n += 1
def get(self, key):
"""Returns the value associated with the specified key.
:param key: the key
:returns: the value associated with the key in the symbol table;
None if no such value
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to get() is None")
i = self._hash(key)
while self.keys[i] is not None:
if self.keys[i] == key:
return self.values[i]
i = (i + 1) % self.m
return None
def delete(self, key):
"""Removes the specified key and its associated value from this symbol table
(if the key is in this symbol table).
:param key: the key
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to delete() is None")
if not self.contains(key):
return
# Find position i of key
i = self._hash(key)
while not key == self.keys[i]:
i = (i + 1) % self.m
# Delete key and associated value
self.keys[i] = None
self.values[i] = None
# Rehash all keys in same cluster
i = (i + 1) % self.m
while self.keys[i] is not None:
# Delete keys[i] and values[i] and reinsert
keyToRehash = self.keys[i]
valueToReash = self.values[i]
self.keys[i] = None
self.values[i] = None
self.n -= 1
self.put(keyToRehash, valueToReash)
i = (i + 1) % self.m
self.n -= 1
# Halves table size if it's less than 12.5% full
if self.n > 0 and self.n <= self.m / 8:
self._resize(self.m // 2)
assert self._check()
def key_list(self):
"""
Returns the keys in the symbol table as an iterable
:returns: A list containing all keys
"""
keys = []
for i in range(0, self.m):
if self.keys[i] is not None:
keys.append(self.keys[i])
return keys
def _check(self):
# Integrity check - don't check after each put() because
# integrity is not maintained during delete()
if self.m < 2 * self.n:
print("Hash table size m = {}; List size n = {}".format(self.m, self.n))
return False
for i in range(0, self.m):
if self.keys[i] is None:
continue
elif self.get(self.keys[i]) != self.values[i]:
print(
"get[{}] = {}; values[i] = {}".format(
self.keys[i], self.get(self.keys[i]), self.values[i]
)
)
return False
return True
def main():
"""Unit tests the LinearProbingHashST data type."""
st = LinearProbingHashST()
i = 1
for key in sys.argv[1:]:
st.put(key, i)
i += 1
for key in st.key_list():
print("{} {}".format(key, st.get(key)))
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/searching/lookup_csv.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
import sys
from itu.algs4.stdlib import stdio
# data files:
# https://algs4.cs.princeton.edu/35applications/amino.csv
# Data files: https://algs4.cs.princeton.edu/35applications/DJIA.csv
# https://algs4.cs.princeton.edu/35applications/UPC.csv
# https://algs4.cs.princeton.edu/35applications/amino.csv
# https://algs4.cs.princeton.edu/35applications/elements.csv
# https://algs4.cs.princeton.edu/35applications/ip.csv
# https://algs4.cs.princeton.edu/35applications/morse.csv
# Execution: python lookup_csv.py file.csv keyField valField
#
# Reads in a set of key-value pairs from a two-column CSV file
# specified on the command line; then, reads in keys from standard
# input and prints out corresponding values.
#
# % python lookup_csv.py amino.csv 0 3 % python lookup_csv.py ip.csv 0 1
# TTA www.google.com
# Leucine 216.239.41.99
# ABC
# Not found % python lookup_csv.py ip.csv 1 0
# TCT 216.239.41.99
# Serine www.google.com
#
# % python lookup_csv.py amino.csv 3 0 % python lookup_csv.py DJIA.csv 0 1
# Glycine 29-Oct-29
# GGG 252.38
# 20-Oct-87
# 1738.74
"""
The LookupCSV class provides a data-driven client for reading in a
key-value pairs from a file; then, printing the values corresponding to the
keys found on standard input. Both keys and values are strings.
The fields to serve as the key and value are taken as command-line arguments.
"""
if __name__ == "__main__":
args = sys.argv[1:]
keyField = int(args[1])
valField = int(args[2])
# symbol table
st = {}
# read in the data from csv file
file = open(args[0], "r")
line = file.readline()
while line:
tokens = line.split(",")
key = tokens[keyField]
val = tokens[valField]
st[key] = val
line = file.readline()
while not stdio.isEmpty():
s = stdio.readString()
if s in st:
print(st.get(s))
else:
print("Not found")
file.close()
================================================
FILE: itu/algs4/searching/lookup_index.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
import sys
from itu.algs4.fundamentals.queue import Queue
from itu.algs4.stdlib import stdio
# is this really useful??
try:
q = Queue()
q.enqueue(1)
except AttributeError:
print("ERROR - Could not import itu.algs4 queue")
sys.exit(1)
# Execution: python lookup_index.py movies.txt "/"
# Dependencies: queue.py stdio.py
# % python lookup_index.py aminoI.csv ","
# Serine
# TCT
# TCA
# TCG
# AGT
# AGC
# TCG
# Serine
#
# % python lookup_index.py movies.txt "/"
# Bacon, Kevin
# Animal House (1978)
# Apollo 13 (1995)
# Beauty Shop (2005)
# Diner (1982)
# Few Good Men, A (1992)
# Flatliners (1990)
# Footloose (1984)
# Friday the 13th (1980)
# ...
# Tin Men (1987)
# DeBoy, David
# Blumenfeld, Alan
# ...
# The LookupIndex class provides a data-driven client for reading in a
# key-value pairs from a file; then, printing the values corresponding to the
# keys found on standard input. Keys are strings; values are lists of strings.
# The separating delimiter is taken as a command-line argument. This client
# is sometimes known as an inverted index.
if __name__ == "__main__":
args = sys.argv[1:]
filename = args[0]
separator = args[1]
file = open(filename, "r")
st = {}
ts = {}
line = file.readline()
while line:
fields = line.split(separator)
key = fields[0]
for i in range(1, len(fields)):
val = fields[i]
if key not in st:
st[key] = Queue()
if val not in ts:
ts[val] = Queue()
st.get(key).enqueue(val)
ts.get(val).enqueue(key)
line = file.readline()
print("Done indexing")
# read queries from standard input, one per line
while not stdio.isEmpty():
query = stdio.readLine()
if query in st:
for vals in st.get(query):
print(" " + vals)
if query in ts:
for keys in ts.get(query):
print(" " + keys)
file.close()
================================================
FILE: itu/algs4/searching/red_black_bst.py
================================================
from abc import abstractmethod
from typing import Generic, Optional, TypeVar
from typing_extensions import Protocol
from ..errors.errors import IllegalArgumentException, NoSuchElementException
from ..fundamentals.queue import Queue
# Created for BADS 2018
# See README.md for details
# Python 3
# Typing ---
Key = TypeVar("Key", bound="Comparable")
Val = TypeVar("Val")
class Comparable(Protocol):
@abstractmethod
def __lt__(self: Key, other: Key) -> bool:
pass
def __le__(self: Key, other: Key) -> bool:
return self < other or self == other
# ---
class Node(Generic[Key, Val]):
"""RedBlackBST helper node data type."""
def __init__(self, key: Key, val: Val, color: bool, size: int):
"""Initializes a new node.
:param key: the key of the node
:param val: the value of the node
:param size: the subtree count
"""
self.key: Key = key
self.val: Val = val
self.left: Optional[Node[Key, Val]] = None
self.right: Optional[Node[Key, Val]] = None
self.size: int = size
self.color: bool = color
class RedBlackBST(Generic[Key, Val]):
"""The RedBlackBST class represents an ordered symbol table of generic key-
value pairs.
It supports the usual put, get, contains, delete, size, and is-empty
methods. It also provides ordered methods for finding the minimum,
maximum, floor, and ceiling. It also provides a keys method for
iterating over all the keys. A symbol table implements the
associative array abstraction: when associating a value with a key
that is already in the symbol table, the convention is to replace
the old value with the new value. This class uses the convention
that values cannot be None-setting the value associated with a key
to None is equivalent to deleting the key from the symbol table.
This implementation uses a left-leaning red-black BST. It requires
that the keys are all of the same type and that they can be
compared. The put, contains, remove, minimum, maximum, ceiling, and
floor operations each take logarithmic time in the worst case, if
the tree becomes unbalanced. The size, and is-empty operations take
constant time. Construction takes constant time.
"""
RED = True
BLACK = False
def __init__(self) -> None:
"""Initializes an empty symbol table."""
self._root: Optional[Node[Key, Val]] = None
def put(self, key: Key, val: Val) -> None:
"""Inserts the specified key-value pair into the symbol table,
overwriting the old value with the new value if the symbol table
already contains the specified key. Deletes the specified key (and its
associated value) from this symbol table if the specified value is
None.
:param key: the key
:param val: the value
:raises IllegalArgumentException: if key is None
"""
# Can never happen if type checked:
if key is None:
raise IllegalArgumentException("first argument to put() is None")
if val is None:
self.delete(key)
return
self._root = self._put(self._root, key, val)
self._root.color = RedBlackBST.BLACK
def _put(self, h: Optional[Node[Key, Val]], key: Key, val: Val) -> Node[Key, Val]:
"""Inserts the key-value pair in the subtree rooted at h.
:param h: root of currently inspected subtree
:param key: the key
:param val: the value
"""
if h is None:
return Node(key, val, self.RED, 1)
if key < h.key:
h.left = self._put(h.left, key, val)
elif key > h.key:
h.right = self._put(h.right, key, val)
else:
h.val = val
assert h is not None
if self._is_red(h.right) and not self._is_red(h.left):
h = self._rotate_left(h)
assert h is not None
if self._is_red(h.left):
assert h.left is not None # bc h.left is red
if self._is_red(h.left.left):
h = self._rotate_right(h)
assert h is not None
if self._is_red(h.left) and self._is_red(h.right):
self._flip_colors(h)
h.size = self._size(h.left) + self._size(h.right) + 1
return h
def get(self, key: Key) -> Optional[Val]:
"""Returns the value associated with the given key.
:param key: the key
:return: the value associated with the given key if the key is in the symbol table
and None if the key is not in the symbol table
:raises IllegalArgumentException: if key is None
"""
# This can never happen if type checked:
if key is None:
raise IllegalArgumentException("argument to get() is None")
return self._get(self._root, key)
def _get(self, x: Optional[Node[Key, Val]], key: Key) -> Optional[Val]:
"""Returns value with the given key in subtree rooted at x. None if no
such key.
:param x: root of currently inspected subtree.
:param key: the key
:return: value associated with given key. None if no such key
"""
while x is not None:
if key < x.key:
x = x.left
elif key > x.key:
x = x.right
else:
return x.val
return None
def delete_min(self) -> None:
"""Removes the smallest key and associated value from the symbol table.
:raises NoSuchElementException: if the symbol table is empty
"""
if self.is_empty():
raise NoSuchElementException("RedBlackBST underflow")
assert self._root is not None
if not self._is_red(self._root.left) and not self._is_red(self._root.right):
self._root.color = self.RED
self._root = self._delete_min(self._root)
if not self.is_empty():
assert self._root is not None
self._root.color = RedBlackBST.BLACK
def _delete_min(self, h: Node[Key, Val]) -> Optional[Node[Key, Val]]:
"""Deletes the node with the minimum key rooted at h."""
if h.left is None:
return None
if not self._is_red(h.left) and not self._is_red(h.left.left):
h = self._move_red_left(h)
assert h.left is not None # because _move_red_left moved something to h.left
h.left = self._delete_min(h.left)
return self._balance(h)
def delete_max(self) -> None:
"""Removes the largest key and associated value from the symbol table.
:raises NoSuchElementException: if the symbol table is empty
"""
if self.is_empty():
raise NoSuchElementException("RedBlackBST underflow")
assert self._root is not None
if not self._is_red(self._root.left) and not self._is_red(self._root.right):
self._root.color = self.RED
self._root = self._delete_max(self._root)
if not self.is_empty():
assert self._root is not None
self._root.color = RedBlackBST.BLACK
def _delete_max(self, h: Node[Key, Val]) -> Optional[Node[Key, Val]]:
"""Deletes the key-value pair with the maximum key rooted at h."""
if self._is_red(h.left):
h = self._rotate_right(h)
if h.right is None:
return None
if not self._is_red(h.right) and not self._is_red(h.right.left):
h = self._move_red_right(h)
assert h.right is not None # because _move_red_right moved something to h.right
h.right = self._delete_max(h.right)
return self._balance(h)
def delete(self, key: Key) -> None:
"""Removes the specified key and its associated value from this symbol
table (if the key is in this symbol table).
:param key: the key
:raises IllegalArgumentException: if key is None
"""
# Cannot happen if type checked:
if key is None:
raise IllegalArgumentException("argument to delete() is None")
if not self.contains(key):
return
assert self._root is not None
if not self._is_red(self._root.left) and not self._is_red(self._root.right):
self._root.color = self.RED
self._root = self._delete(self._root, key)
if not self.is_empty():
assert self._root is not None
self._root.color = RedBlackBST.BLACK
def _delete(self, h: Node[Key, Val], key: Key) -> Optional[Node[Key, Val]]:
"""Deletes the key-value pair with the given key rooted at h."""
if key < h.key:
# we assert (from delete) that key exists in h's subtree, so it must exists
# in the left subtree, so h.left is not None
assert h.left is not None
if not self._is_red(h.left) and not self._is_red(h.left.left):
h = self._move_red_left(h)
assert h.left is not None # _move_red_left does what it should
h.left = self._delete(h.left, key)
else:
if self._is_red(h.left):
h = self._rotate_right(h)
if key == h.key and h.right is None:
return None
assert h.right is not None # bc. key must be in the right subtree
if not self._is_red(h.right) and not self._is_red(h.right.left):
h = self._move_red_right(h)
assert h.right is not None
if key == h.key:
x = self._min(h.right)
h.key = x.key
h.val = x.val
h.right = self._delete_min(h.right)
else:
h.right = self._delete(h.right, key)
return self._balance(h)
def size(self) -> int:
"""Return the number of key-value pairs in this symbol table.
:return: the number of key-value pairs in this symbol table
"""
return self._size(self._root)
def __len__(self) -> int:
return self.size()
def _size(self, x: Optional[Node[Key, Val]]) -> int:
"""Number of nodes in subtree rooted at x. 0 if x is None.
:param x: root node of subtree
:return: number of nodes in subtree
"""
if x is None:
return 0
return x.size
def contains(self, key: Key) -> bool:
"""Does this symbol table contain the given key?
:param key: the key
:return: True if this symbol table contains key and False otherwise
"""
return self.get(key) is not None
def is_empty(self) -> bool:
"""Is this symbol table empty?
:return: True if this symbol table is empty and False otherwise
"""
return self._root is None
@classmethod
def _is_red(self, x: Optional[Node[Key, Val]]) -> bool:
"""Is node x red?
:param x: the node to check
:return: True if node is red False otherwise
"""
if x is None:
return False
return x.color == RedBlackBST.RED
def _rotate_left(self, h: Node[Key, Val]) -> Node[Key, Val]:
"""Make a right-leaning link lean to the left.
:param h:
:return: The node that has taken h's position
"""
x = h.right
assert x is not None # bc h has a right-leaning (red) link
h.right = x.left
x.left = h
x.color = h.color
h.color = self.RED
x.size = h.size
h.size = self._size(h.left) + self._size(h.right) + 1
return x
def _rotate_right(self, h: Node[Key, Val]) -> Node[Key, Val]:
"""Make a left-leaning link lean to the right.
:param h:
:return: The node that has taken h's position
"""
x = h.left
assert x is not None # bc h has a left-leaning (red) link
h.left = x.right
x.right = h
x.color = h.color
h.color = self.RED
x.size = h.size
h.size = self._size(h.left) + self._size(h.right) + 1
return x
def _flip_colors(self, h: Node[Key, Val]) -> None:
"""Flip the colors of a node and its two children.
:param h: the node
"""
assert h.left is not None
assert h.right is not None
h.color = not h.color
h.left.color = not h.left.color
h.right.color = not h.right.color
def _move_red_left(self, h: Node[Key, Val]) -> Node[Key, Val]:
"""Assuming that h is red and both h.left and h.left.left are black,
make h.left or one of its children red.
Assumes h.right exists
"""
assert h.right is not None
self._flip_colors(h)
if self._is_red(h.right.left):
h.right = self._rotate_right(h.right)
h = self._rotate_left(h)
self._flip_colors(h)
return h
def _move_red_right(self, h: Node[Key, Val]) -> Node[Key, Val]:
"""Assuming that h is red and both h.right and h.right.left are black,
make h.right or one of its children red."""
assert h.left is not None # more is true: h.right.left exists and is not red
self._flip_colors(h)
if self._is_red(h.left.left):
h = self._rotate_right(h)
self._flip_colors(h)
return h
def _balance(self, h: Node[Key, Val]) -> Node[Key, Val]:
"""Restore red-black tree invariant."""
if self._is_red(h.right) and not self._is_red(h.left):
h = self._rotate_left(h)
if self._is_red(h.left):
assert h.left is not None
if self._is_red(h.left.left):
h = self._rotate_right(h)
if self._is_red(h.left) and self._is_red(h.right):
self._flip_colors(h)
h.size = self._size(h.left) + self._size(h.right) + 1
return h
def height(self) -> int:
"""
Returns the height of the RedBlackBST
:return: the height of the RedBlackBST (a 1-node tree has height 0)
"""
return self._height(self._root)
def _height(self, x: Optional[Node[Key, Val]]) -> int:
"""Returns height of subtree rooted at x."""
if x is None:
return -1
return 1 + max(self._height(x.left), self._height(x.right))
def min(self) -> Key:
"""
Returns the smallest key in the symbol table.
:return: the smallest key in the symbol table
:raises NoSuchElementException: if the symbol table is empty
"""
if self.is_empty():
raise NoSuchElementException("calls min() with empty symbol table")
assert self._root is not None
return self._min(self._root).key
def _min(self, x: Node[Key, Val]) -> Node[Key, Val]:
"""Returns the Node with the smallest key in subtree rooted at x.
None if no such key
"""
if x.left is None:
return x
else:
return self._min(x.left)
def max(self) -> Key:
"""
Returns the largest key in the symbol table.
:return: the largest key in the symbol table
:raises NoSuchElementException: if the symbol table is empty
"""
if self.is_empty():
raise NoSuchElementException("calls max() with empty symbol table")
assert self._root is not None
return self._max(self._root).key
def _max(self, x: Node[Key, Val]) -> Node[Key, Val]:
"""Returns the node with the largest key in subtree rooted at x.
None if no such key.
"""
if x.right is None:
return x
else:
return self._max(x.right)
def keys(self) -> Queue[Key]:
"""Returns all keys in the symbol table.
:return: all keys in the symbol table
"""
if self.is_empty():
return Queue()
return self.keys_range(self.min(), self.max())
def keys_range(self, lo: Key, hi: Key) -> Queue[Key]:
"""Returns all keys in the symbol table in the given range.
:param lo: minimum endpoint
:param hi: maximum endpoint
:return: all keys in the symbol table between lo (inclusive) and hi (inclusive)
:raises IllegalArgumentException: if either lo or hi is None
"""
if lo is None:
raise IllegalArgumentException("first argument to keys() is None")
if hi is None:
raise IllegalArgumentException("second argument to keys() is None")
queue: Queue[Key] = Queue()
self._keys(self._root, queue, lo, hi)
return queue
def _keys(
self, x: Optional[Node[Key, Val]], queue: Queue[Key], lo: Key, hi: Key
) -> None:
"""Adds the keys between lo and hi in the subtree rooted at x to the
queue."""
if x is None:
return
if lo < x.key:
self._keys(x.left, queue, lo, hi)
if not x.key < lo and x.key <= hi:
queue.enqueue(x.key)
if hi > x.key:
self._keys(x.right, queue, lo, hi)
def select(self, k: int) -> Key:
"""Return the kth smallest key in the symbol table.
:param k: the order statistic
:return: the kth smallest key in the symbol table
:raises IllegalArgumentException: unless k is between 0 and n-1
"""
if k < 0 or k >= self.size():
raise IllegalArgumentException(
"argument to select() is invalid: {}".format(k)
)
assert self._root is not None # bc. 0 <= k < self.size()
x = self._select(self._root, k)
return x.key
def _select(self, x: Node[Key, Val], k: int) -> Node[Key, Val]:
"""Returns the node with key of rank k in the subtree rooted at x."""
t = self._size(x.left)
if t > k:
assert x.left is not None
return self._select(x.left, k)
elif t < k:
assert x.right is not None
return self._select(x.right, k - t - 1)
else:
return x
def rank(self, key: Key) -> int:
"""Returns the number of keys in the symbol table strictly less than
key.
:param key: the key
:return: the number of keys in the symbol table strictly less than key
:raises IllegalArgumentException: if key is None
"""
if key is None:
raise IllegalArgumentException("argument to rank() is None")
return self._rank(key, self._root)
def _rank(self, key: Key, x: Optional[Node[Key, Val]]) -> int:
"""Returns the number of keys less than key in the subtree rooted at
x."""
if x is None:
return 0
if key < x.key:
return self._rank(key, x.left)
if key > x.key:
return 1 + self._size(x.left) + self._rank(key, x.right)
else:
return self._size(x.left)
def size_range(self, lo: Key, hi: Key) -> int:
"""Returns the number of keys in the symbol table in the given range.
:param lo: minimum endpoint
:param hi: maximum endpoint
:return: the number of keys in the symbol table between lo
(inclusive) and hi (inclusive)
:raises IllegalArgumentException: if either lo or hi is None
"""
if lo is None:
return IllegalArgumentException("first argument to size() is None")
if hi is None:
return IllegalArgumentException("second argument to size() is None")
if lo > hi:
return 0
if self.contains(hi):
return self.rank(hi) - self.rank(lo) + 1
else:
return self.rank(hi) - self.rank(lo)
def floor(self, key: Key) -> Key:
"""Returns the largest key in the symbol table less than or equal to
key.
:param key: the key
:return: the largest key in the symbol table less than er equal to key
:raises IllegalArgumentException: if key is None
:raises NoSuchElementException: if there is no such key
"""
if key is None:
raise IllegalArgumentException("argument to floor() is None")
if self.is_empty():
raise NoSuchElementException("calls floor() with empty symbol table")
x = self._floor(self._root, key)
if x is None:
raise NoSuchElementException("calls floor() with key < min")
return x.key
def _floor(self, x: Optional[Node[Key, Val]], key: Key) -> Optional[Node[Key, Val]]:
"""Returns the largest key in the subtree rooted at x less than or
equal to the given key."""
if x is None:
return None
if key == x.key:
return x
if key < x.key:
return self._floor(x.left, key)
t = self._floor(x.right, key)
if t is not None:
return t
return x
def ceiling(self, key: Key) -> Key:
"""Returns the smallest key in the symbol table greater than or equal
to key.
:param key: the key
:return: the smallest key in the symbol table greater than or equal to key
:raises IllegalArgumentException: if key is None
:raises NoSuchElementException: if there is no such key
"""
if key is None:
raise IllegalArgumentException("argument to ceiling is None")
if self.is_empty():
raise NoSuchElementException("calls ceiling() with empty symbol table")
x = self._ceiling(self._root, key)
if x is None:
raise NoSuchElementException("calls ceiling() with key > max")
return x.key
def _ceiling(
self, x: Optional[Node[Key, Val]], key: Key
) -> Optional[Node[Key, Val]]:
"""Returns the node with the smallest key in the subtree rooted at x
greater than or equal to the given key."""
if x is None:
return None
assert x is not None
if key == x.key:
return x
if key > x.key:
return self._ceiling(x.right, key)
t = self._ceiling(x.left, key)
if t is not None:
return t
return x
================================================
FILE: itu/algs4/searching/seperate_chaining_hst.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
import sys
from itu.algs4.searching.sequential_search_st import SequentialSearchST
class SeparateChainingHashST:
"""The SeparateChainingHashST class represents a symbol table of dynamic key-
value pairs. It supports the usual put, get, contains, delete, size, and is-
empty methods. It also provides a keys method for iterating over all of the
keys. A symbol table implements the associative array abstraction: when
associating a value with a key that is already in the symbol table, the
convention is to replace the old value with the new value. Unlike the Map-
class in Java, this class uses the convention that values cannot be null/None.
Setting the value associated with a key to None is equivalent to deleting the
key from the symbol table.
This implementation uses a separate chaining hash table. It requires
that the key type overrides the __eq__ and __hash__ methods. The
expected time per put, contains, or remove operation is constant,
subject to the uniform hashing assumption. The size, and is-empty
operations take constant time. Construction takes constant time.
"""
def __init__(self, M=997):
self.M = M # Hash table size
self.N = 0 # Number of pairs
self.st = [SequentialSearchST() for i in range(0, M)] # Array of ST objects
def _hash(self, key):
# Hash value between 0 and M-1
return (hash(key) & 0x7FFFFFFF) % self.M
def get(self, key):
"""Returns the value associated with the specified key.
:param key: the key
:returns: the value associated with the key in the symbol table;
None if no such value
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to get() is None")
i = self._hash(key)
return self.st[i].get(key)
def put(self, key, value):
"""Inserts the specified key-value paur into the symbol table, overwriting
the old value with the new value if the symbol table already contains the
specified key. Deletes the specified key (and its associated value) from this
symbol table if the specified value is None.
:param key: the key
:param value: the value
:raises ValueError: if key is None.
"""
if key is None:
raise ValueError("first argument to put() is None")
if value is None:
self.delete(key)
return
i = self._hash(key)
if not self.st[i].contains(key):
self.N += 1
self.st[i].put(key, value)
self.st[self._hash(key)].put(key, value)
def size(self):
"""Returns the number of key-value pairs in this symbol table.
:returns: the number of key-value pairs in this symbol table.
"""
return self.N
def is_empty(self):
"""Returns true if the symbol table is empty.
:returns: True if this symbol table is empty;
False otherwise.
"""
return self.N == 0
def contains(self, key):
"""Returns true if this symbol table contains the specified key.
:param key: the key
:returns: True if this symbol table contains the key;
False otherwise.
:raises ValueError: if key is None.
"""
if key is None:
raise ValueError("argument to contains() is None")
return self.get(key) is not None
def delete(self, key):
"""Removes the specified key and its associated value from this symbol table
(if the key is in this symbol table).
:param key: the key
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to delete() is None")
i = self._hash(key)
if self.st[i].contains(key):
self.N -= 1
self.st[i].delete(key)
def keys(self):
"""
Returns the keys in the symbol table as an iterable
:returns: A list containing all keys
"""
keys = []
for i in range(0, self.M):
for key in self.st[i].keys():
keys.append(key)
return keys
def __len__(self):
return self.size()
def main():
"""Unit tests the SeparateChainingHashST data type."""
st = SeparateChainingHashST()
i = 0
for key in sys.argv[1:]:
st.put(key, i)
i += 1
for key in st.keys():
print("{} {}".format(key, st.get(key)))
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/searching/sequential_search_st.py
================================================
# Created for BADS 2018
# see README.md for details
# This is python3
class SequentialSearchST:
"""The SequentialSearchST class represents an (unordered) symbol table of
generic key-value pairs. It supports the usual put, get, contains, delete,
size, and is-empty methods. It also provides a keys method for iterating
over all of the keys. A symbol table implements the associative array
abstraction: when associating a value with a key that is already in the
symbol table, the convention is to replace the old value with the new
value. The class also uses the convention that values cannot be None.
Setting the value associated with a key to None is equivalent to deleting
the key from the symbol table.
This implementation uses a singly-linked list and sequential search.
It relies on the equals() method to test whether two keys are
equal. It does not call either the compareTo() or hashCode()
method. The put and delete operations take linear time the get and
contains operations takes linear time in the worst case. The size,
and is-empty operations take constant time. Construction takes
constant time.
"""
class Node:
# a helper linked list data type
def __init__(self, key, val, next):
self.key = key
self.val = val
self.next = next
def __init__(self):
"""Initializes an empty symbol table."""
self._n = 0 # number of key-value pairs
self._first = None # the linked list of key-value pairs
def size(self):
"""Returns the number of key-value pairs in this symbol table.
:returns: the number of key-value pairs in this symbol table
"""
return self._n
def __len__(self):
return self.size()
def is_empty(self):
"""Returns true if this symbol table is empty.
:returns: true if this symbol table is empty
false otherwise
"""
return self.size() == 0
def contains(self, key):
"""Returns true if this symbol table contains the specified key.
:param key the key
:returns: true if this symbol table contains key
false otherwise
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to contains() is None")
return self.get(key) is not None
def get(self, key):
"""Returns the value associated with the given key in this symbol
table.
:param key: the key
:returns: the value associated with the given key if the key is in the symbol table
and None if the key is not in the symbol table
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to get() is None")
x = self._first
while x is not None:
if key == x.key:
return x.val
x = x.next
return None
def put(self, key, val):
"""Inserts the specified key-value pair into the symbol table,
overwriting the old value with the new value if the symbol table
already contains the specified key. Deletes the specified key (and its
associated value) from this symbol table if the specified value is
None.
:param key: the key
:param val: the value
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to put() is None")
if val is None:
self.delete(key)
return
x = self._first
while x is not None:
if key == x.key:
x.val = val
return
x = x.next
self._first = self.Node(key, val, self._first)
self._n += 1
def delete(self, key):
"""Removes the specified key and its associated value from this symbol
table (if the key is in this symbol table).
:param key the key
:raises ValueError: if key is None
"""
if key is None:
raise ValueError("argument to delete() is None")
self._first = self._delete(self._first, key)
def _delete(self, x, key):
# delete key in linked list beginning at Node x
# warning: function call stack too large if table is large
if x is None:
return None
if key == x.key:
self._n -= 1
return x.next
x.next = self._delete(x.next, key)
return x
def keys(self):
"""Returns all keys in the symbol table as an Iterable. To iterate
over all of the keys in the symbol table named st, use the foreach
notation: for Key key in st.keys().
:returns: all keys in the symbol table
"""
x = self._first
while x is not None:
yield x.key
x = x.next
if __name__ == "__main__":
from itu.algs4.stdlib import stdio
st = SequentialSearchST()
i = 0
while not stdio.isEmpty():
key = stdio.readString()
st.put(key, i)
i += 1
for s in st.keys():
stdio.writef("%s %i\n", s, st.get(s))
================================================
FILE: itu/algs4/searching/set.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
from itu.algs4.errors.errors import (
IllegalArgumentException,
NoSuchElementException,
UnsupportedOperationException,
)
"""
Set implementation using Python's set() type.
Does not allow duplicates.
"""
class SET:
# Initializes a new set that is an independent copy of the specified set, or an empty one.
def __init__(self, x=None):
self._set = set() if x is None else x._set.copy()
# Adds the key to this set (if it is not already present).
def add(self, key):
if key is None:
raise IllegalArgumentException("called add() with a None key")
self._set.add(key)
# Returns true if this set contains the given key.
def contains(self, key):
if key is None:
raise IllegalArgumentException("called contains() with a None key")
return key in self._set
# Removes the specified key from this set (if the set contains the specified key).
def delete(self, key):
if key is None:
raise IllegalArgumentException("called delete() with a None key")
if self.contains(key):
self._set.remove(key)
# Returns the number of keys in this set.
def size(self):
return len(self._set)
def __len__(self):
return self.size()
# Returns true if this set is empty.
def is_empty(self):
return self.size() == 0
# Returns all of the keys in this set, as an iterator.
# To iterate over all of the keys in a set named set, use the
# foreach notation: for key in set.
def __iter__(self):
for k in self._set:
yield k
# Returns the largest key in this set.
def max(self):
if self.is_empty():
raise NoSuchElementException("called max() with empty set")
return max(self._set)
# Returns the smallest key in this set.
def min(self):
if self.is_empty():
raise NoSuchElementException("called min() with empty set")
return min(self._set)
# Returns the smallest key in this set greater than or equal to key.
def ceiling(self, key):
if key is None:
raise IllegalArgumentException("called ceiling() with None key")
ceiling = None
for k in self:
if (ceiling is None and k >= key) or (
ceiling is not None and k >= key and k < ceiling
):
ceiling = k
if ceiling is None:
raise NoSuchElementException("all keys are less than " + str(key))
return ceiling
# Returns the largest key in this set less than or equal to key.
def floor(self, key):
if key is None:
raise IllegalArgumentException("called floor() with None key")
floor = None
for k in self:
if (floor is None and k <= key) or (
floor is not None and k <= key and k > floor
):
floor = k
if floor is None:
raise NoSuchElementException("all keys are greater than " + str(key))
return floor
# Returns the union of this set and that set.
def union(self, that):
if that is None:
raise IllegalArgumentException("called union() with a None argument")
c = set()
for x in self:
c.add(x)
for x in that:
c.add(x)
return c
# Returns the intersection of this set and that set.
def intersects(self, that):
if that is None:
raise IllegalArgumentException("called intersects() with a null argument")
c = set()
if self.size() < that.size():
for x in self:
if that.contains(x):
c.add(x)
else:
for x in that:
if self.contains(x):
c.add(x)
return c
# Compares this set to the specified set.
def __eq__(self, other):
if other is None:
return False
if type(other) is not type(self):
return False
return self._set == other._set
# This operation is not supported because sets are mutable.
def hashCode(self):
raise UnsupportedOperationException(
"hashCode() is not supported because sets are mutable"
)
# Returns a string representation of this set.
def __repr__(self):
s = []
for item in self:
s.append("{}".format(item))
return "{ " + "".join(s) + " }"
def main():
set = SET()
print("set = " + str(set))
# insert some keys
set.add("www.cs.princeton.edu")
set.add("www.cs.princeton.edu") # overwrite old value
set.add("www.princeton.edu")
set.add("www.math.princeton.edu")
set.add("www.yale.edu")
set.add("www.amazon.com")
set.add("www.simpsons.com")
set.add("www.stanford.edu")
set.add("www.google.com")
set.add("www.ibm.com")
set.add("www.apple.com")
set.add("www.slashdot.com")
set.add("www.whitehouse.gov")
set.add("www.espn.com")
set.add("www.snopes.com")
set.add("www.movies.com")
set.add("www.cnn.com")
set.add("www.iitb.ac.in")
print(set.contains("www.cs.princeton.edu"))
print(not set.contains("www.harvardsucks.com"))
print(set.contains("www.simpsons.com"))
print()
print("ceiling(www.simpsonr.com) = " + set.ceiling("www.simpsonr.com"))
print("ceiling(www.simpsons.com) = " + set.ceiling("www.simpsons.com"))
print("ceiling(www.simpsont.com) = " + set.ceiling("www.simpsont.com"))
print("floor(www.simpsonr.com) = " + set.floor("www.simpsonr.com"))
print("floor(www.simpsons.com) = " + set.floor("www.simpsons.com"))
print("floor(www.simpsont.com) = " + set.floor("www.simpsont.com"))
print()
print("set = " + str(set))
print()
# print out all keys in this set in lexicographic order
for s in set:
print(s)
print()
set2 = SET(set)
print(set == set2)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/searching/sparse_vector.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
from math import sqrt
from itu.algs4.searching.seperate_chaining_hst import SeparateChainingHashST
class SparseVector:
"""The SparseVector class represents a d-dimensional mathematical vector.
Vectors are mutable: their values can be changed after they are created. It
includes methods for addition, subtraction, dot product, scalar product, unit
vector and Euclidean norm.
The implementation is a symbol table of indices and values for which
the vector coordinates are nonzero. This makes it efficient when most
of the vector coordinates are zero.
"""
def __init__(self, d):
"""Initializes a d-dimensional zero vector.
:param d: the dimension of the vector
"""
self.d = d
self.st = SeparateChainingHashST()
def put(self, i, value):
"""Sets the ith coordinate of this vector to the specified value.
:param i: the index
:param value: the new value
:raises ValueError: unless i is between 0 and d-1
"""
if i < 0 or i >= self.d:
raise ValueError("Illegal index")
if value == 0.0:
self.st.delete(i)
else:
self.st.put(i, value)
def get(self, i):
"""Returns the ith coordinate of this vector.
:param i: the index
:returns: the value of the ith coordinate of this vector
:raises ValueError: unless i is between 0 and d-1
"""
if i < 0 or i >= self.d:
raise ValueError("Illegal index")
if self.st.contains(i):
return self.st.get(i)
else:
return 0.0
def nnz(self):
"""Returns the number of nonzero entries in this vector.
:returns: the number of nonzero entries in this vector.
"""
return self.st.size()
def dimension(self):
"""Returns the dimension of this vector.
:returns: the dimension of this vector.
"""
return self.d
def dot(self, that):
"""Returns the inner product of this vector with the specified vector.
:param that: the other vector
:returns: the dot product between this vector and that vector
:raises ValueError: if the lengths of the two vectors are not equal
"""
if self.d != that.d:
raise ValueError("Vector lengths disagree")
sum = 0.0
# iterate over the vector with the fewest nonzeroes
if self.st.size() <= that.st.size():
for i in self.st.keys():
if that.st.contains(i):
sum += self.get(i) * that.get(i)
else:
for i in that.st.keys():
if self.st.contains(i):
sum += self.get(i) * that.get(i)
return sum
def magnitude(self):
"""Returns the magnitude of this vector. This is also known as the L2 norm or
the Euclidean norm.
:returns: the magnitude of this vector
"""
return sqrt(self.dot(self))
def scale(self, alpha):
"""Returns the scalar-vector product of this vector with the specified
scalar.
:param alpha: the scalar
:returns: the scalar-vector product of this vector with the specified scalar
"""
c = SparseVector(self.d)
for i in self.st.keys():
c.put(i, alpha * self.get(i))
return c
def plus(self, that):
"""Returns the sum of this vector and the specified vector.
:param that: the vector to add to this vector
:returns: the sum of this vector and that vector
:raises ValueError: if the dimension of the two vectors are not equal
"""
if self.d != that.d:
raise ValueError("Vector lengths disagree")
c = SparseVector(self.d)
for i in self.st.keys():
c.put(i, self.get(i))
for i in that.st.keys():
c.put(i, that.get(i) + c.get(i))
return c
def __repr__(self):
return "".join(("(%s,%s)" % (str(i), self.st.get(i))) for i in self.st.keys())
def main():
"""Unit tests the SparseVector data type."""
a = SparseVector(10)
b = SparseVector(10)
a.put(3, 0.50)
a.put(9, 0.75)
a.put(6, 0.11)
a.put(6, 0.00)
b.put(3, 0.60)
b.put(4, 0.90)
print("a = {}".format(a))
print("b = {}".format(b))
print("a dot b = {}".format(a.dot(b)))
print("a + b = {}".format(a.plus(b)))
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/searching/st.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
from itu.algs4.errors.errors import IllegalArgumentException, NoSuchElementException
"""
The ST class represents an ordered symbol table of generic
key-value pairs.
"""
class ST:
def __init__(self):
self._st = dict()
# Returns the value associated with the given key in this symbol table.
def get(self, key):
if key is None:
raise IllegalArgumentException("called get() with None key")
return self._st.get(key)
# Inserts the specified key-value pair into the symbol table, overwriting the old
# value with the new value if the symbol table already contains the specified key.
# Deletes the specified key (and its associated value) from this symbol table
# if the specified value is None.
def put(self, key, val):
if key is None:
raise IllegalArgumentException("called put() with None key")
if val is None:
self._st.pop(key, None)
else:
self._st[key] = val
# Removes the specified key and its associated value from this symbol table
# (if the key is in this symbol table).
def delete(self, key):
if key is None:
raise IllegalArgumentException("called delete() with None key")
self._st.pop(key, None)
# Returns true if this symbol table contain the given key.
def contains(self, key):
if key is None:
raise IllegalArgumentException("called contains() with None key")
return key in self._st
# Returns the number of key-value pairs in this symbol table.
def size(self):
return len(self._st)
def __len__(self):
return self.size()
# Returns true if this symbol table is empty.
def is_empty(self):
return self.size() == 0
# Returns all keys in this symbol table.
# To iterate over all of the keys in the symbol table named st,
# use the foreach notation: for key in st.keys() .
def keys(self):
return self._st.keys()
def __iter__(self):
for k in self.keys():
yield k
# Returns the smallest key in this symbol table.
def min(self):
if self.is_empty():
raise NoSuchElementException("called min() with empty symbol table")
return min(self._st)
# Returns the largest key in this symbol table.
def max(self):
if self.is_empty():
raise NoSuchElementException("called max() with empty symbol table")
return max(self._st)
# Returns the smallest key in this symbol table greater than or equal to key.
def ceiling(self, key):
if key is None:
raise IllegalArgumentException("called ceiling() with None key")
keys = self.keys()
ceiling = None
for k in keys:
if (ceiling is None and k >= key) or (
ceiling is not None and k >= key and k < ceiling
):
ceiling = k
if ceiling is None:
raise NoSuchElementException("all keys are less than " + str(key))
return ceiling
# Returns the largest key in this symbol table less than or equal to key.
def floor(self, key):
if key is None:
raise IllegalArgumentException("called floor() with None key")
keys = self.keys()
floor = None
for k in keys:
if (floor is None and k <= key) or (
floor is not None and k <= key and k > floor
):
floor = k
if floor is None:
raise NoSuchElementException("all keys are greater than " + str(key))
return floor
================================================
FILE: itu/algs4/sorting/__init__.py
================================================
================================================
FILE: itu/algs4/sorting/datafiles/tiny.txt
================================================
S O R T E X A M P L E
================================================
FILE: itu/algs4/sorting/datafiles/words3.txt
================================================
bed bug dad yes zoo
now for tip ilk dim
tag jot sob nob sky
hut men egg few jay
owl joy rap gig wee
was wad fee tap tar
dug jam all bad yet
================================================
FILE: itu/algs4/sorting/heap.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
from itu.algs4.stdlib import stdio
"""
The heap module provides a function for heapsorting an array.
"""
def sort(pq):
"""Rearranges the array in ascending order, using the natural order.
:param pq: the array to be sorted
"""
n = len(pq)
for k in range(n // 2, 0, -1):
_sink(pq, k, n)
while n > 1:
_exch(pq, 1, n)
n -= 1
_sink(pq, 1, n)
def _sink(pq, k, n):
"""Moves item at index k down to a legal position on the heap.
:param k: Index of the item to be moved
:param n: Amount of items left on the heap
"""
while 2 * k <= n:
j = 2 * k
if j < n and _less(pq, j, j + 1):
j += 1
if not _less(pq, k, j):
break
_exch(pq, k, j)
k = j
def _less(pq, i, j):
"""Check if item at index i is greater than item at index j on the heap.
Indices are "off-by-one" to support 1-based indexing.
:param pq: the heap
:param i: index of the first item
:param j: index of the second item
:return: True if item at index i is smaller than item at index j otherwise False
"""
return pq[i - 1] < pq[j - 1]
def _exch(pq, i, j):
"""Exchanges the positions of items at index i and j on the heap. Indices
are "off-by-one" to support 1-based indexing.
:param pq: the heap
:param i: index of the first item
:param j: index of the second item
"""
pq[i - 1], pq[j - 1] = pq[j - 1], pq[i - 1]
def _show(pq):
"""Print the contents of the array.
:param pq: the array to be printed
"""
for i in range(len(pq)):
print(pq[i])
def main():
"""Reads in a sequence of strings from stdin heapsorts them, and prints the
result in ascending order."""
a = stdio.readAllStrings()
sort(a)
_show(a)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/sorting/index_min_pq.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
from itu.algs4.errors.errors import IllegalArgumentException, NoSuchElementException
class IndexMinPQ:
"""The IndexMinPQ class represents an indexed priority queue of generic
keys. It supports the usual insert and delete-the-minimum operations, along
with delete and change-the-key methods. In order to let the client refer to
the keys on the priority queue,
an integer between 0 and maxN - 1
is associated with each key-the client uses this integer to specify
which key to delete or change.
It also supports methods for peeking at the minimum key,
testing if the priority queue is empty, and iterating through
the keys.
This implementation uses a binary heap along with an array to associate
keys with integers, in the given range.
The insert, delete-the-minimum, delete, change-key, decrease-key, and increase-key
operations take logarithmic time.
The is-empty, size, min-index, min-key, and key-of operations take constant time.
Construction takes time proportional to the specified capacity.
"""
def __init__(self, max_n):
"""Initializes an empty indexed priority queue with indices between 0.
and max_n - 1.
:param max_n: the keys on this priority queue are indices from 0 to max_n - 1
:raises IllegalArgumentException: if max_n < 0
"""
self.max_n = max_n
self.n = 0
self.keys = [None] * (max_n + 1)
self.pq = [0] * (max_n + 1)
self.qp = [-1] * (max_n + 1)
def insert(self, i, key):
"""Associates key with index i.
:param i: an index
:param key: the key to associate with index i
:raises IllegalArgumentException: unless 0 <= i < max_n
:raises IllegalArgumentException: if there already is an item associated with index i
"""
if i < 0 or i >= self.max_n:
raise IllegalArgumentException("index is not within range")
if self.contains(i):
raise IllegalArgumentException("index is already in the priority queue")
self.n += 1
self.qp[i] = self.n
self.pq[self.n] = i
self.keys[i] = key
self._swim(self.n)
def contains(self, i):
"""Is i an index on this priority queue?
:param i: an index
:return: True if i is an index on this priority queue False otherwise
:rtype: bool
:raises IllegalArgumentException: unless 0 <= i < max_n
"""
if i < 0 or i >= self.max_n:
raise IllegalArgumentException("index is not within range")
return self.qp[i] != -1
def change_key(self, i, key):
"""Change the key associated with index i to the specified value.
:param i: the index of the key to change
:param key: change the key associated with index i to this key
:raises IllegalArgumentException: unless 0 <= i < max_n
:raises NoSuchElementException: if no key is associated with index i
"""
if i < 0 or i >= self.max_n:
raise IllegalArgumentException("index is not within range")
if not self.contains(i):
raise NoSuchElementException("index is not in the priority queue")
self.keys[i] = key
self._swim(self.qp[i])
self._sink(self.qp[i])
def decrease_key(self, i, key):
"""Decrease the key associated with index i to the specified value.
:param i: the index of the key to decrease
:param key: decrease the key associated with index i to this key
:raises IllegalArgumentException: unless 0 <= i < max_n
:raises IllegalArgumentException: if key >= key_of(i)
:raises NoSuchElementException: if no key is associated with index i
"""
if i < 0 or i >= self.max_n:
raise IllegalArgumentException("index is not within range")
if not self.contains(i):
raise IllegalArgumentException("index is not in the priority queue")
if self.keys[i] <= key:
raise IllegalArgumentException(
"calling decrease_key() with given argument would not strictly decrease the key"
)
self.keys[i] = key
self._swim(self.qp[i])
def increase_key(self, i, key):
"""Increase the key associated with index i to the specified value.
:param i: the index of the key to increase
:param key: increase the key associated with index i to this key
:raises IllegalArgumentException: unless 0 <= i < max_n
:raises IllegalArgumentException: if key <= key_of(i)
:raises NoSuchElementException: if no key is associated with index i
"""
if i < 0 or i >= self.max_n:
raise IllegalArgumentException("index is not within range")
if not self.contains(i):
raise NoSuchElementException("index is not in the priority queue")
if self.keys[i] >= key:
raise IllegalArgumentException(
"calling increase_key() with given argument would not strictly increase the key"
)
self.keys[i] = key
self._sink(self.qp[i])
def delete(self, i):
"""Remove the key associated with index i.
:param i: the index of the key to remove
:raises IllegalArgumentException: unless 0 <= i < max_n
:raises NoSuchElementException: if no key is associated with index i
"""
if i < 0 or i >= self.max_n:
raise IllegalArgumentException("index is not in range")
if not self.contains(i):
raise NoSuchElementException("index is not in the priority queue")
index = self.qp[i]
self._exch(index, self.n)
self.n -= 1
self._sink(index)
self.keys[i] = None
self.qp[i] = -1
def min_index(self):
"""
Returns an index associated with a minimum key.
:return: an index associated with a minimum key
:rtype: int
:raises NoSuchElementException: if this priority queue is empty
"""
if self.n == 0:
raise NoSuchElementException("Priority queue underflow")
return self.pq[1]
def min_key(self):
"""
Returns a minimum key.
:return: a minimum key
:raises NoSuchElementException: if this priority queue is empty
"""
if self.n == 0:
raise NoSuchElementException("Priority queue underflow")
return self.keys[self.pq[1]]
def del_min(self):
"""
Removes a minimum key and returns its associated index.
:return: an index associated with a minimum key
:raises NoSuchElementException: if this priority queue is empty
:rtype: int
"""
if self.n == 0:
raise NoSuchElementException("Priority queue underflow")
_min = self.pq[1]
self._exch(1, self.n)
self.n -= 1
self._sink(1)
self.qp[_min] = -1
self.keys[_min] = None
self.pq[self.n + 1] = -1
return _min
def is_empty(self):
"""Returns True if this priority queue is empty.
:return: True if this priority queue is empty False otherwise
:rtype: bool
"""
return self.n == 0
def size(self):
"""Returns the number of keys on this priority queue.
:return: the number of keys on this priority queue
:rtype: int
"""
return self.n
def __len__(self):
return self.size()
def key_of(self, i):
"""Returns the key associated with index i.
:param i: the index of the key to return
:return: the key associated with index i
:raises IllegalArgumentException: unless 0 <= i < max_n
:raises NoSuchElementException: if no key is associated with index i
"""
if i < 0 or i >= self.max_n:
raise IllegalArgumentException("index is out of range")
if not self.contains(i):
raise IllegalArgumentException("index is not on the priority queue")
return self.keys[i]
def _exch(self, i, j):
"""Exchanges the position of items at index i and j on the heap.
:param i: index of the first item
:param j: index of the second item
"""
self.pq[i], self.pq[j] = self.pq[j], self.pq[i]
self.qp[self.pq[i]] = i
self.qp[self.pq[j]] = j
def _greater(self, i, j):
"""Returns True if key at index i on the heap is greater than key at
index j.
:param i: index of the first item
:param j: index of the second item
:return: True if key at index i on the heap is greater than key at index j
:rtype: bool
"""
return self.keys[self.pq[i]] > self.keys[self.pq[j]]
def _swim(self, k):
"""Moves item at index k up to a legal position on the heap.
:param k: Index of the item on the heap to be moved
"""
while k > 1 and self._greater(k // 2, k):
self._exch(k, k // 2)
k = k // 2
def _sink(self, k):
"""Moves item at index k down to a legal position on the heap.
:param k: Index of the item on the heap to be moved
"""
while 2 * k <= self.n:
j = 2 * k
if j < self.n and self._greater(j, j + 1):
j += 1
if not self._greater(k, j):
break
self._exch(k, j)
k = j
def __iter__(self):
"""Iterates over all the items in this priority queue in ascending
order."""
copy = IndexMinPQ(len(self.pq) - 1)
for i in range(1, self.n + 1):
copy.insert(self.pq[i], self.keys[self.pq[i]])
while not copy.is_empty():
yield copy.del_min()
def main():
"""Inserts a bunch of strings to an indexed priority queue, deletes and
prints them, inserts them again, and prints them using an iterator."""
strings = ["it", "was", "the", "best", "of", "times", "it", "was", "the", "worst"]
pq = IndexMinPQ(len(strings))
for i in range(len(strings)):
pq.insert(i, strings[i])
while not pq.is_empty():
i = pq.del_min()
print("{} {}".format(i, strings[i]))
print()
for i in range(len(strings)):
pq.insert(i, strings[i])
for i in pq:
print("{} {}".format(i, strings[i]))
while not pq.is_empty():
pq.del_min()
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/sorting/insertion_sort.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
"""The Insertion module provides static methods for sorting an array using
insertion sort.
This implementation makes ~ 1/2 n^2 compares and exchanges in the worst
case, so it is not suitable for sorting large arbitrary arrays. More
precisely, the number of exchanges is exactly equal to the number of
inversions. So, for example, it sorts a partially-sorted array in linear
time. The sorting algorithm is stable and uses O(1) extra memory.
"""
import sys
from typing import List, TypeVar
T = TypeVar("T")
def sort(a: List[T]):
"""Rearranges the array in ascending order, using the natural order.
:param a: the array to be sorted.
"""
# Sort a[] into increasing order.
N = len(a)
for i in range(1, N):
# Insert a[i] among a[i-1], a[i-2], a[i-3]...
for j in range(i, 0, -1):
if not _less(a[j], a[j - 1]):
break
_exch(a, j, j - 1)
def _less(v: T, w: T):
return v < w
def _exch(a: List[T], i: int, j: int):
t = a[i]
a[i] = a[j]
a[j] = t
def _show(a: List[T]):
# Prints the array on a single line
for item in a:
print(item, end=" ")
print()
def is_sorted(a: List[T]):
"""Returns true if a is sorted.
:param a: the array to be checked.
:returns: True if a is sorted.
"""
for i in range(1, len(a)):
if _less(a[i], a[i - 1]):
return False
return True
def main():
"""Reads in a sequence of strings from standard input; Shellsorts them; and
prints them to standard output in ascending order."""
a = sys.argv[1:]
sort(a)
assert is_sorted(a)
_show(a)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/sorting/max_pq.py
================================================
# Created for BADS 2018
# see README.md for details
# Python 3
from typing import Generic, Iterator, List, Optional, TypeVar
from itu.algs4.errors.errors import NoSuchElementException
from itu.algs4.stdlib import stdio
Key = TypeVar("Key")
class MaxPQ(Generic[Key]):
"""The MaxPQ class represents a priority queue of generic keys.
It supports the usual insert and delete-the-maximum operations,
along with methods for peeking at the maximum key, testing if the
priority queue is empty, and iterating through the keys. This
implementation uses a binary heap. The insert and delete-the-maximum
operations take logarithmic amortized time. The max, size and
is_empty operations take constant time. Construction takes time
proportional to the specified capacity.
"""
def __init__(self, _max: int = 1):
"""Initializes an empty priority queue with the given initial capacity.
:param _max: the initial capacity, default value is 1
"""
self._pq: List[Optional[Key]] = [None] * (_max + 1)
self._n = 0
def insert(self, x: Key) -> None:
"""Adds a new key to this priority queue.
:param x: the new key to add to this priority queue
"""
if self._n == len(self._pq) - 1:
self._resize(2 * len(self._pq))
self._n += 1
self._pq[self._n] = x
self._swim(self._n)
def max(self) -> Key:
"""
Returns a largest key on this priority queue.
:return: a largest key on the priority queue
:raises NoSuchElementException: if this priority queue is empty
"""
if self.is_empty():
raise NoSuchElementException("Priority queue underflow")
assert self._pq[1] is not None
return self._pq[1]
def del_max(self) -> Key:
"""
Removes and returns a largest key on this priority queue.
:return: a largest key on this priority queue
:raises NoSuchElementException: if this priority queue is empty
"""
if self.is_empty():
raise NoSuchElementException("Priority queue underflow")
_max = self._pq[1]
assert _max is not None
self._exch(1, self._n)
self._n -= 1
self._sink(1)
self._pq[self._n + 1] = None
if self._n > 0 and self._n == (len(self._pq) - 1) // 4:
self._resize(len(self._pq) // 2)
return _max
def is_empty(self) -> bool:
"""Returns True if this priority queue is empty.
:return: True if this priority queue is empty otherwise False
:rtype: bool
"""
return self._n == 0
def size(self) -> int:
"""Returns the number of keys on this priority queue.
:return: the number of keys on this priority queue
:rtype: int
"""
return self._n
def __len__(self) -> int:
return self.size()
def _sink(self, k) -> None:
"""Moves item at index k down to a legal position on the heap.
:param k: Index of the item to be moved
"""
while 2 * k <= self._n:
j = 2 * k
if j < self._n and self._less(j, j + 1):
j += 1
if not self._less(k, j):
break
self._exch(k, j)
k = j
def _swim(self, k: int) -> None:
"""Moves item at index k up to a legal position on the heap.
:param k: Index of the item to be moved
"""
while k > 1 and self._less(k // 2, k):
self._exch(k, k // 2)
k = k // 2
def _resize(self, capacity: int):
"""Copies the contents of the heap to a new array of size capacity.
:param capacity: The capacity of the new array
"""
temp: List[Optional[Key]] = [None] * capacity
for i in range(1, self._n + 1):
temp[i] = self._pq[i]
self._pq = temp
def _less(self, i: int, j: int):
"""Check if item at index i is less than item at index j on the heap.
:param i: index of the first item
:param j: index of the second item
:return: True if item at index i is smaller than item at index j otherwise False
"""
return self._pq[i] < self._pq[j]
def _exch(self, i: int, j: int):
"""Exchanges the position of items at index i and j on the heap.
:param i: index of the first item
:param j: index of the second item
"""
self._pq[i], self._pq[j] = self._pq[j], self._pq[i]
def __iter__(self) -> Iterator[Key]:
"""Iterates over all the items in this priority queue in heap order."""
copy: MaxPQ[Key] = MaxPQ(self.size())
for i in range(1, self._n + 1):
key = self._pq[i]
assert key is not None
copy.insert(key)
for i in range(1, copy._n + 1):
yield copy.del_max()
def main():
"""Reads strings from stdin and adds them to a priority queue.
When reading a '-' it removes a maximum item on the priority queue
and prints it to stdout. Prints the amount of items left on the
priority queue
"""
pq = MaxPQ()
while not stdio.isEmpty():
item = stdio.readString()
if item != "-":
pq.insert(item)
elif not pq.is_empty():
print(pq.del_max())
print("({} left on pq)".format(pq.size()))
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/sorting/merge.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
from typing import List, Optional
"""
This module provides functions for sorting an array using mergesort.
For additional documentation, see Section 2.2 of Algorithms, 4th Edition
by Robert Sedgewick and Kevin Wayne.
"""
# Sorts a sequence of strings from standard input using mergesort
def _is_sorted(a: List, lo: int = 0, hi: Optional[int] = None):
# If hi is not specified, use whole array
if hi is None:
hi = len(a)
# check if sublist is sorted
for i in range(lo + 1, hi):
if a[i] < a[i - 1]:
return False
return True
# stably merge a[lo .. mid] with a[mid+1 ..hi] using aux[lo .. hi]
def _merge(a: List, aux: List, lo: int, mid: int, hi: int):
# precondition: a[lo .. mid] and a[mid+1 .. hi] are sorted subarrays
assert _is_sorted(a, lo, mid)
assert _is_sorted(a, mid + 1, hi)
# copy to aux[]
for k in range(lo, hi + 1):
aux[k] = a[k]
# merge back to a[]
i, j = lo, mid + 1
for k in range(lo, hi + 1):
if i > mid:
a[k] = aux[j]
j += 1
elif j > hi:
a[k] = aux[i]
i += 1
elif aux[j] < aux[i]:
a[k] = aux[j]
j += 1
else:
a[k] = aux[i]
i += 1
# postcondition: a[lo .. hi] is sorted
assert _is_sorted(a, lo, hi)
# mergesort a[lo..hi] using auxiliary array aux[lo..hi]
def _sort(a: List, aux: List, lo: int, hi: int):
if hi <= lo:
return
mid = lo + (hi - lo) // 2
_sort(a, aux, lo, mid)
_sort(a, aux, mid + 1, hi)
_merge(a, aux, lo, mid, hi)
def sort(a: List):
"""Rearranges the array in ascending order, using the natural order.
:param a: the array to be sorted
"""
aux = [None] * len(a)
_sort(a, aux, 0, len(a) - 1)
assert _is_sorted(a)
# Reads in a sequence of strings from standard input or a file
# supplied as argument to the program; mergesorts them;
# and prints them to standard output in ascending order.
if __name__ == "__main__":
import sys
from itu.algs4.stdlib import stdio
if len(sys.argv) > 1:
try:
sys.stdin = open(sys.argv[1])
except IOError:
print("File not found, using standard input instead")
a = stdio.readAllStrings()
sort(a)
assert _is_sorted(a)
for elem in a:
print(elem)
================================================
FILE: itu/algs4/sorting/merge_bu.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
"""This module provides functions for sorting an array using bottom-up
mergesort.
For additional documentation, see Section 2.1 of Algorithms, 4th Edition
by Robert Sedgewick and Kevin Wayne.
"""
# Sorts a sequence of strings from standard input using mergesort
def _is_sorted(a, lo=0, hi=None):
# If hi is not specified, use whole array
if hi is None:
hi = len(a)
# check if sublist is sorted
for i in range(lo + 1, hi):
if a[i] < a[i - 1]:
return False
return True
# stably merge a[lo .. mid] with a[mid+1 ..hi] using aux[lo .. hi]
def _merge(a, aux, lo, mid, hi):
# copy to aux[]
for k in range(lo, hi + 1):
aux[k] = a[k]
# merge back to a[]
i, j = lo, mid + 1
for k in range(lo, hi + 1):
if i > mid:
a[k] = aux[j]
j += 1
elif j > hi:
a[k] = aux[i]
i += 1
elif aux[j] < aux[i]:
a[k] = aux[j]
j += 1
else:
a[k] = aux[i]
i += 1
def sort(a):
"""Rearranges the array in ascending order, using the natural order.
:param a: the array to be sorted
"""
n = len(a)
aux = [None] * n
length = 1
while length < n:
lo = 0
while lo < n - length:
mid = lo + length - 1
hi = min(mid + length, n - 1)
_merge(a, aux, lo, mid, hi)
lo += 2 * length
length *= 2
assert _is_sorted(a)
# Reads in a sequence of strings from standard input or a file
# supplied as argument to the program; mergesorts them;
# and prints them to standard output in ascending order.
if __name__ == "__main__":
import sys
from itu.algs4.stdlib import stdio
if len(sys.argv) > 1:
try:
sys.stdin = open(sys.argv[1])
except IOError:
print("File not found, using standard input instead")
a = stdio.readAllStrings()
sort(a)
for elem in a:
print(elem)
================================================
FILE: itu/algs4/sorting/min_pq.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
from typing import Generic, List, Optional, TypeVar
from itu.algs4.errors.errors import NoSuchElementException
from itu.algs4.stdlib import stdio
Key = TypeVar("Key")
class MinPQ(Generic[Key]):
"""The MinPQ class represents a priority queue of generic keys.
It supports the usual insert and delete-the-minimum operations,
along with methods for peeking at the minimum key, testing if the
priority queue is empty, and iterating through the keys. This
implementation uses a binary heap. The insert and delete-the-minimum
operations take logarithmic amortized time. The min, size and is-
empty operations take constant time. Construction takes time
proportional to the specified capacity.
"""
def __init__(self, _max: int = 1) -> None:
"""Initializes an empty priority queue with the given initial capacity.
:param _max: the initial capacity, default value is 1
"""
self._pq: List[Optional[Key]] = [None] * (_max + 1)
self._n = 0
def insert(self, x: Key) -> None:
"""Adds a new key to this priority queue.
:param x: the new key to add to this priority queue
"""
if self._n == len(self._pq) - 1:
self._resize(2 * len(self._pq))
self._n += 1
self._pq[self._n] = x
self._swim(self._n)
def min(self) -> Key:
"""
Returns a smallest key on this priority queue.
:return: a smallest key on the priority queue
:raises NoSuchElementException: if this priority queue is empty
"""
if self.is_empty():
raise NoSuchElementException("Priority queue underflow")
assert self._pq[1] is not None
return self._pq[1]
def del_min(self) -> Key:
"""
Removes and returns a smallest key on this priority queue.
:return: a smallest key on this priority queue
:raises NoSuchElementException: if this priority queue is empty
"""
if self.is_empty():
raise NoSuchElementException("Priority queue underflow")
_min = self._pq[1]
assert _min is not None
self._exch(1, self._n)
self._n -= 1
self._sink(1)
self._pq[self._n + 1] = None
if self._n > 0 and self._n == (len(self._pq) - 1) // 4:
self._resize(len(self._pq) // 2)
return _min
def is_empty(self) -> bool:
"""Returns True if this priority queue is empty.
:return: True if this priority queue is empty otherwise False
:rtype: bool
"""
return self._n == 0
def size(self) -> int:
"""Returns the number of keys on this priority queue.
:return: the number of keys on this priority queue
:rtype: int
"""
return self._n
def __len__(self) -> int:
return self.size()
def _sink(self, k) -> None:
"""Moves item at index k down to a legal position on the heap.
:param k: Index of the item to be moved
"""
while 2 * k <= self._n:
j = 2 * k
if j < self._n and self._greater(j, j + 1):
j += 1
if not self._greater(k, j):
break
self._exch(k, j)
k = j
def _swim(self, k) -> None:
"""Moves item at index k up to a legal position on the heap.
:param k: Index of the item to be moved
"""
while k > 1 and self._greater(k // 2, k):
self._exch(k, k // 2)
k = k // 2
def _greater(self, i: int, j: int):
"""Check if item at index i is greater than item at index j on the
heap.
:param i: index of the first item
:param j: index of the second item
:return: True if item at index i is smaller than item at index j otherwise False
"""
return self._pq[i] > self._pq[j]
def _resize(self, capacity: int):
"""Copies the contents of the heap to a new array of size capacity.
:param capacity: The capacity of the new array
"""
temp: List[Optional[Key]] = [None] * capacity
for i in range(1, self._n + 1):
temp[i] = self._pq[i]
self._pq = temp
def _exch(self, i: int, j: int):
"""Exchanges the position of items at index i and j on the heap.
:param i: index of the first item
:param j: index of the second item
"""
self._pq[i], self._pq[j] = self._pq[j], self._pq[i]
def __iter__(self):
"""Iterates over all the items in this priority queue in ascending
order."""
copy = MinPQ(self.size())
for i in range(1, self._n + 1):
copy.insert(self._pq[i])
for i in range(1, copy._n + 1):
yield copy.del_min()
def main():
"""Reads strings from stdin and adds them to a minimum priority queue.
When reading a '-' it removes the minimum element and prints it to
stdout.
"""
pq = MinPQ()
while not stdio.isEmpty():
item = stdio.readString()
if item != "-":
pq.insert(item)
elif not pq.is_empty():
print(pq.del_min())
print("({} left on pq)".format(pq.size()))
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/sorting/quick3way.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
"""The Quick3Way module provides static methods for sorting an array using
quicksort with 3-way partitioning."""
import sys
from random import shuffle
def sort(a):
"""Rearranges the array in ascending order using the natural order.
:param a: the array to be sorted.
"""
shuffle(a) # Eliminate dependency on input.
_sort(a, 0, len(a) - 1)
def _sort(a, lo, hi):
if hi <= lo:
return
lt = lo
i = lo + 1
gt = hi
v = a[lo]
while i <= gt:
cmpr = _compare(a[i], v)
if cmpr < 0:
_exch(a, lt, i)
lt += 1
i += 1
elif cmpr > 0:
_exch(a, i, gt)
gt -= 1
else:
i += 1
_sort(a, lo, lt - 1)
_sort(a, gt + 1, hi)
assert is_sorted(a)
def _compare(a, b):
return (a > b) - (b > a)
def _less(v, w):
return _compare(v, w) < 0
def _exch(a, i, j):
t = a[i]
a[i] = a[j]
a[j] = t
def _show(a):
# Prints the array on a single line
for item in a:
print(item, end=" ")
print()
def is_sorted(a):
"""Returns true if a is sorted.
:param a: the array to be checked.
:returns: True if a is sorted.
"""
for i in range(1, len(a)):
if _less(a[i], a[i - 1]):
return False
return True
def main():
"""Reads in a sequence of strings from standard input; Shellsorts them; and
prints them to standard output in ascending order."""
a = sys.argv[1:]
sort(a)
_show(a)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/sorting/quicksort.py
================================================
# Created for BADS 2018
# see README.md for details
# Python 3
"""The quicksort module provides methods for sorting an array and selecting the
ith smallest element in an array using quicksort.
For additional documentation, see Section 2.3 of Algorithms, 4th Edition
by Robert Sedgewick and Kevin Wayne.
:original author: Robert Sedgewick and Kevin Wayne
:original java code: https://algs4.cs.princeton.edu/23quicksort/Quick.java.html
"""
from itu.algs4.stdlib import stdio, stdrandom
def sort(array):
"""Rearranges the array in ascending order, using the natural order."""
stdrandom.shuffle(array)
_sort(array, 0, len(array) - 1)
# quicksort the subarray from array[lo] to array[hi]
def _sort(array, lo, hi):
if hi <= lo:
return
j = _partition(array, lo, hi)
_sort(array, lo, j - 1)
_sort(array, j + 1, hi)
# partition the subarray array[lo..hi] so that
# array[lo..j-1] <= array[j] <= array[j+1..hi]
# and return the index j
def _partition(array, lo, hi):
i = lo
j = hi + 1
v = array[lo]
while True:
while array[i + 1] < v:
i += 1
if i == hi:
break
i += 1
# find item on hi to swap
while v < array[j - 1]:
j -= 1
if j == lo:
break
j -= 1
# check if pointers cross
if i >= j:
break
_exch(array, i, j)
# put partitioning item v at a[j]
_exch(array, lo, j)
# now array[lo .. j-1] <= a[j] <= a[j+1 .. hi]
return j
def select(array, k):
"""Rearranges the array so that array[k] contains the kth smalles key;
array[0] through array[k-1] are less than (or equal to) array[k]; and
array[k+1] through array[n-1] are greather than (or equal to) array[k]
:param array: the array
:param k: the rank of the key
:return: the key of rank k
"""
stdrandom.shuffle(array)
lo = 0
hi = len(array) - 1
while hi > lo:
i = _partition(array, lo, hi)
if i > k:
hi = i - 1
elif i < k:
lo = i + 1
else:
return array[i]
return array[lo]
# exchange array[i] and array[j]
def _exch(array, i, j):
swap = array[i]
array[i] = array[j]
array[j] = swap
###########################################################
##### Check if array is sorted - useful for debugging #####
###########################################################
def is_sorted(array):
return _is_sorted(array, 0, len(array) - 1)
def _is_sorted(array, lo, hi):
for i in range(lo + 1, hi + 1):
if array[i] < array[i - 1]:
return False
return True
# print array to standard output
def show(array):
stdio.write(" ".join(array))
if __name__ == "__main__":
array = stdio.readAllStrings()
sort(array)
assert is_sorted(array)
show(array)
# shuffle
stdrandom.shuffle(array)
# display results again using select
print()
for i in range(0, len(array)):
ith = str(select(array, i))
stdio.writeln(ith)
================================================
FILE: itu/algs4/sorting/selection.py
================================================
from itu.algs4.stdlib import stdio
# Created for BADS 2018
# see README.md for details
# Python 3
"""
The selection module provides a function for sorting an
array using selection sort.
"""
def sort(a):
"""Rearranges the array in ascending order, using the natural order.
:param a: the array to be sorted
"""
_n = len(a)
for i in range(_n):
_min = i
for j in range(i + 1, _n):
if a[j] < a[_min]:
_min = j
_exch(a, i, _min)
def _exch(a, i, j):
"""Exchanges the the items at index i and j.
:param a: the array to be sorted
:param i: the index for the first item
:param j: the index for the second item
"""
a[i], a[j] = a[j], a[i]
def _show(a):
"""Prints the content of the array.
:param a: The array to be shown
"""
for i in range(len(a)):
print(a[i])
def main():
"""Reads strings from stdin, sorts them, and prints the result to
stdout."""
a = stdio.readAllStrings()
sort(a)
_show(a)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/sorting/shellsort.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
"""The Shellsort module provides static methods for sorting an array using
shellsort with Knuth's increment sequence (1, 4, 13, 40, ...)."""
import sys
def sort(a):
"""Rearranges the array in ascending order using the natural order.
:param a: the array to be sorted.
"""
N = len(a)
h = 1
while h < int(N / 3):
h = (3 * h) + 1
while h >= 1:
# h-sort the array
for i in range(h, N):
# Insert a[i] among a[i-h], a[i-2*h], a[i-3*h]...
for j in range(i, h - 1, -h):
if not _less(a[j], a[j - h]):
break
_exch(a, j, j - h)
h = int(h / 3)
def _less(v, w):
return v < w
def _exch(a, i, j):
t = a[i]
a[i] = a[j]
a[j] = t
def _show(a):
# Prints the array on a single line
for item in a:
print(item, end=" ")
print()
def is_sorted(a):
"""Returns true if a is sorted.
:param a: the array to be checked.
:returns: True if a is sorted.
"""
for i in range(1, len(a)):
if _less(a[i], a[i - 1]):
return False
return True
def main():
"""Reads in a sequence of strings from standard input; Shellsorts them; and
prints them to standard output in ascending order."""
a = sys.argv[1:]
sort(a)
assert is_sorted(a)
_show(a)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/stdlib/__init__.py
================================================
"""This module is based on the code at
https://introcs.cs.princeton.edu/python/code/ written by Robert Sedgewick,
Kevin Wayne, and Robert Dondero."""
================================================
FILE: itu/algs4/stdlib/binary_out.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
import struct
import sys
"""
Binary output. This class provides methods for converting
some primitive type variables (boolean, byte, char, and int)
to sequences of bits and writing them
to an output stream.
The output stream can be standard output or another outputstream.
Uses big-endian (most-significant byte first).
The client must flush() the output stream when finished writing bits.
The client should not intermix calls to BinaryOut with calls to stdout;
otherwise unexpected behavior will result.
"""
class BinaryOut:
def __init__(self, os=sys.stdout):
"""Initializes a binary output stream from a specified output stream.
Defaults to stdin.
:param os: the output streamt to write to.
"""
self.out = os.buffer
self.buffer = 0 # 8-bit buffer of bits to write out
self.n = 0 # number of bits used in buffer
def _writeBit(self, x):
self.buffer <<= 1
if x:
self.buffer |= 1
self.n += 1
if self.n == 8:
self._clearBuffer()
def _writeByte(self, x):
assert x >= 0 and x < 256
# optimized if byte-alligned
if self.n == 0:
self.out.write(struct.pack("B", x))
return
# otherwise write one bit at a time
for i in range(0, 8):
bit = ((x >> (8 - i - 1)) & 1) == 1
self._writeBit(bit)
def _clearBuffer(self):
if self.n == 0:
return
if self.n > 0:
self.buffer <<= 8 - self.n
self.out.write(self.buffer.to_bytes(1, "big"))
self.n = 0
self.buffer = 0
def flush(self):
self._clearBuffer()
self.out.flush()
def close(self):
self.flush()
self.out.close()
def write_bool(self, x):
self._writeBit(x)
def write_byte(self, x):
self._writeByte(x & 0xFF)
def write_int(self, x):
self._writeByte(((x >> 24) & 0xFF))
self._writeByte(((x >> 16) & 0xFF))
self._writeByte(((x >> 8) & 0xFF))
self._writeByte(((x >> 0) & 0xFF))
def write_char(self, x):
if ord(x) < 0 or ord(x) >= 256:
raise ValueError("Illegal 8-bit char = {}".format(x))
self._writeByte(ord(x))
def write_string(self, s):
for i in s:
self.write_char(s[i])
def main():
out = BinaryOut()
for i in sys.argv[1]:
out.write_char(i)
out.close()
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/stdlib/binary_stdin.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
import struct
import sys
from itu.algs4.stdlib.binary_stdout import BinaryStdOut
"""
Binary standard input. This class provides methods for reading
in bits from standard input, either one bit at a time (as a boolean),
8 bits at a time (as a char) or 32 bits at a time (as an int)
All primitive types are assumed to be represented in big-endian order.
The client should not mix class to BinaryStdIn with calls to stdin,
otherwise unexpected behavior will result.
"""
class BinaryStdIn:
EOF = -1
ins = sys.stdin.buffer
n = 0
buffer_ = 0
is_init = False
@staticmethod
def _initialize():
BinaryStdIn.buffer_ = 0
BinaryStdIn.n = 0
BinaryStdIn._fill_buffer()
BinaryStdIn.is_init = True
@staticmethod
def _fill_buffer():
x = BinaryStdIn.ins.read(1)
if x == b"":
BinaryStdIn.buffer_ = BinaryStdIn.EOF
BinaryStdIn.n = -1
return
BinaryStdIn.buffer_ = struct.unpack("B", x)[0]
BinaryStdIn.n = 8
@staticmethod
def close():
"""Close this input stream and release any associated system resources."""
if not BinaryStdIn.is_init:
BinaryStdIn._initialize()
BinaryStdIn.ins.close()
BinaryStdIn.is_init = False
@staticmethod
def is_empty():
if not BinaryStdIn.is_init:
BinaryStdIn._initialize()
return BinaryStdIn.buffer_ == BinaryStdIn.EOF
@staticmethod
def read_bool():
if BinaryStdIn.is_empty():
raise EOFError("Reading from empty input stream")
BinaryStdIn.n -= 1
bit = ((BinaryStdIn.buffer_ >> BinaryStdIn.n) & 1) == 1
if BinaryStdIn.n == 0:
BinaryStdIn._fill_buffer()
return bit
@staticmethod
def read_char():
if BinaryStdIn.is_empty():
raise EOFError("Reading from empty input stream")
if BinaryStdIn.n == 8:
x = BinaryStdIn.buffer_
BinaryStdIn._fill_buffer()
return chr(x & 0xFF)
x = BinaryStdIn.buffer_
x <<= 8 - BinaryStdIn.n
oldN = BinaryStdIn.n
if BinaryStdIn.is_empty():
raise EOFError("Reading from empty input stream")
BinaryStdIn._fill_buffer()
BinaryStdIn.n = oldN
x |= BinaryStdIn.buffer_ >> BinaryStdIn.n
return chr(x & 0xFF)
@staticmethod
def read_string():
if BinaryStdIn.is_empty():
raise EOFError("Reading from empty input stream")
sb = ""
while not BinaryStdIn.is_empty():
sb += BinaryStdIn.read_char()
return sb
@staticmethod
def read_int(r=32):
if r == 32:
x = 0
for _ in range(0, 4):
c = BinaryStdIn.read_char()
x <<= 8
x |= ord(c)
return x
if r < 1 or r > 32:
raise ValueError("Illegal value for r = {}".format(r))
x = 0
for _ in range(0, r):
x <<= 1
bit = BinaryStdIn.read_bool()
if bit:
x |= 1
return x
def main():
while not BinaryStdIn.is_empty():
BinaryStdOut.write_char(BinaryStdIn.read_char())
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/stdlib/binary_stdout.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
import struct
import sys
"""
Binary standard output. This class provides methods for converting
some primitive type variables (boolean, byte, char, and int)
to sequences of bits and writing them
to an output stream.
The output stream can be standard output or another outputstream.
Uses big-endian (most-significant byte first).
The client must flush() the output stream when finished writing bits.
The client should not intermix calls to BinaryOut with calls to stdout;
otherwise unexpected behavior will result.
"""
class BinaryStdOut:
out = sys.stdout.buffer
buffer_ = 0
n = 0
is_init = False
@staticmethod
def _initialize():
BinaryStdOut.buffer_ = 0 # 8-bit buffer of bits to write out
BinaryStdOut.n = 0 # number of bits used in buffer
BinaryStdOut.is_init = True
@staticmethod
def _write_bit(x):
if not BinaryStdOut.is_init:
BinaryStdOut._initialize()
BinaryStdOut.buffer_ <<= 1
if x:
BinaryStdOut.buffer_ |= 1
BinaryStdOut.n += 1
if BinaryStdOut.n == 8:
BinaryStdOut._clear_buffer()
@staticmethod
def _write_byte(x):
if not BinaryStdOut.is_init:
BinaryStdOut._initialize()
assert x >= 0 and x < 256
# optimized if byte-alligned
if BinaryStdOut.n == 0:
BinaryStdOut.out.write(struct.pack("B", x))
return
# otherwise write one bit at a time
for i in range(0, 8):
bit = ((x >> (8 - i - 1)) & 1) == 1
BinaryStdOut._write_bit(bit)
@staticmethod
def _clear_buffer():
if not BinaryStdOut.is_init:
BinaryStdOut._initialize()
if BinaryStdOut.n == 0:
return
if BinaryStdOut.n > 0:
BinaryStdOut.buffer_ <<= 8 - BinaryStdOut.n
BinaryStdOut.out.write(struct.pack("B", BinaryStdOut.buffer_))
BinaryStdOut.n = 0
BinaryStdOut.buffer_ = 0
@staticmethod
def flush():
BinaryStdOut._clear_buffer()
BinaryStdOut.out.flush()
@staticmethod
def close():
BinaryStdOut.flush()
BinaryStdOut.out.close()
BinaryStdOut.is_init = False
@staticmethod
def write_bool(x):
BinaryStdOut._write_bit(x)
@staticmethod
def write_byte(x):
BinaryStdOut._write_byte(x & 0xFF)
@staticmethod
def write_int(x, r=32):
if r == 32:
BinaryStdOut._write_byte(((x >> 24) & 0xFF))
BinaryStdOut._write_byte(((x >> 16) & 0xFF))
BinaryStdOut._write_byte(((x >> 8) & 0xFF))
BinaryStdOut._write_byte(((x >> 0) & 0xFF))
return
if r < 1 or r > 16:
raise ValueError("Illegal value for r = {}".format(r))
if x < 0 or x >= (1 << r):
raise ValueError("Illegal {}-bit char = {}".format(r, x))
for i in range(0, r):
bit = ((x >> (r - i - 1)) & 1) == 1
BinaryStdOut._write_bit(bit)
@staticmethod
def write_char(x, r=8):
if r == 8:
if ord(x) < 0 or ord(x) >= 256:
raise ValueError("Illegal 8-bit char = {}".format(x))
BinaryStdOut._write_byte(ord(x))
return
if r < 1 or r > 16:
raise ValueError("Illegal value for r = {}".format(r))
if ord(x) >= (1 << r):
raise ValueError("Illegal {}-bit char = {}".format(r, x))
for i in range(0, r):
bit = ((x >> (r - i - 1)) & 1) == 1
BinaryStdOut._write_bit(bit)
def write_string(s, r=8):
for i in s:
BinaryStdOut.write_char(i, r)
def main():
for i in sys.argv[1]:
BinaryStdOut.write_char(i)
BinaryStdOut.close()
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/stdlib/color.py
================================================
# code based on https://introcs.cs.princeton.edu/python/code/stdlib-python.zip as downloaded in dec 2017
"""color.py.
The color module defines the Color class and some popular Color objects.
"""
# -----------------------------------------------------------------------
class Color:
"""A Color object models an RGB color."""
# -------------------------------------------------------------------
def __init__(self, r=0, g=0, b=0):
"""Construct self such that it has the given red (r), green (g), and
blue (b) components."""
self._r = r # Red component
self._g = g # Green component
self._b = b # Blue component
# -------------------------------------------------------------------
def getRed(self):
"""Return the red component of self."""
return self._r
# -------------------------------------------------------------------
def getGreen(self):
"""Return the green component of self."""
return self._g
# -------------------------------------------------------------------
def getBlue(self):
"""Return the blue component of self."""
return self._b
# -------------------------------------------------------------------
def __str__(self):
"""Return the string equivalent of self, that is, a string of the form
'(r, g, b)'."""
# return '#%02x%02x%02x' % (self._r, self._g, self._b)
return "(" + str(self._r) + ", " + str(self._g) + ", " + str(self._b) + ")"
# -----------------------------------------------------------------------
# Some predefined Color objects:
WHITE = Color(255, 255, 255)
BLACK = Color(0, 0, 0)
RED = Color(255, 0, 0)
GREEN = Color(0, 255, 0)
BLUE = Color(0, 0, 255)
CYAN = Color(0, 255, 255)
MAGENTA = Color(255, 0, 255)
YELLOW = Color(255, 255, 0)
DARK_RED = Color(128, 0, 0)
DARK_GREEN = Color(0, 128, 0)
DARK_BLUE = Color(0, 0, 128)
GRAY = Color(128, 128, 128)
DARK_GRAY = Color(64, 64, 64)
LIGHT_GRAY = Color(192, 192, 192)
ORANGE = Color(255, 200, 0)
VIOLET = Color(238, 130, 238)
PINK = Color(255, 175, 175)
# Shade of blue used in Introduction to Programming in Java.
# It is Pantone 300U. The RGB values are approximately (9, 90, 166).
BOOK_BLUE = Color(9, 90, 166)
BOOK_LIGHT_BLUE = Color(103, 198, 243)
# Shade of red used in Algorithms 4th edition
BOOK_RED = Color(150, 35, 31)
# -----------------------------------------------------------------------
def _main():
"""For testing:"""
from itu.algs4.stdlib import stdio
c1 = Color(128, 128, 128)
stdio.writeln(c1)
stdio.writeln(c1.getRed())
stdio.writeln(c1.getGreen())
stdio.writeln(c1.getBlue())
if __name__ == "__main__":
_main()
================================================
FILE: itu/algs4/stdlib/instream.py
================================================
# code based on https://introcs.cs.princeton.edu/python/code/stdlib-python.zip as downloaded in dec 2017
"""instream.py.
The instream module defines the InStream class.
"""
# -----------------------------------------------------------------------
import re
import sys
if sys.hexversion < 0x03000000:
import urllib
else:
from urllib import request as urllib
# -----------------------------------------------------------------------
class InStream:
"""An InStream object wraps around a text file or sys.stdin, and supports
reading from that stream.
Note: Usually it's a bad idea to mix these three sets of methods:
-- isEmpty(), readInt(), readFloat(), readBool(), readString()
-- hasNextLine(), readLine()
-- readAll(), readAllInts(), readAllFloats(), readAllBools(),
readAllStrings(), readAllLines()
Usually it's better to use one set exclusively.
"""
# -------------------------------------------------------------------
def __init__(self, fileOrUrl=None):
"""Construct self to wrap around a stream.
The stream can be a file whose name is given as fileOrUrl, a
resource whose URL is given as fileOrUrl, or sys.stdin by
default.
"""
self._buffer = ""
self._stream = None
self._readingWebPage = False
if fileOrUrl is None:
# To change the mode of sys.stdin:
from itu.algs4.stdlib import stdio # noqa: F401
self._stream = sys.stdin
return
# Try to open a file, then a URL.
try:
if sys.hexversion < 0x03000000:
self._stream = open(fileOrUrl, "rU")
else:
self._stream = open(fileOrUrl, "r", encoding="utf-8")
except IOError:
try:
self._stream = urllib.urlopen(fileOrUrl)
self._readingWebPage = True
except IOError:
raise IOError("No such file or URL: " + fileOrUrl)
# -------------------------------------------------------------------
def _readRegExp(self, regExp):
"""Discard leading white space characters from the stream wrapped by
self.
Then read from the stream and return a string matching regular
expression regExp. Raise an EOFError if no non-whitespace
characters remain in the stream. Raise a ValueError if the next
characters to be read from the stream do not match regExp.
"""
if self.isEmpty():
raise EOFError()
compiledRegExp = re.compile(r"^\s*" + regExp)
match = compiledRegExp.search(self._buffer)
if match is None:
raise ValueError()
s = match.group()
self._buffer = self._buffer[match.end() :]
return s.lstrip()
# -------------------------------------------------------------------
def isEmpty(self):
"""Return True iff no non-whitespace characters remain in the stream
wrapped by self."""
while self._buffer.strip() == "":
line = self._stream.readline()
if sys.hexversion < 0x03000000 or self._readingWebPage:
line = line.decode("utf-8")
if line == "":
return True
self._buffer += str(line)
return False
# -------------------------------------------------------------------
def readInt(self):
"""Discard leading white space characters from the stream wrapped by
self.
Then read from the stream a sequence of characters comprising an
integer. Convert the sequence of characters to an integer, and
return the integer. Raise an EOFError if no non-whitespace
characters remain in the stream. Raise a ValueError if the next
characters to be read from the stream cannot comprise an
integer.
"""
s = self._readRegExp(r"[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)")
radix = 10
strLength = len(s)
if (strLength >= 1) and (s[0:1] == "0"):
radix = 8
if (strLength >= 2) and (s[0:2] == "-0"):
radix = 8
if (strLength >= 2) and (s[0:2] == "0x"):
radix = 16
if (strLength >= 2) and (s[0:2] == "0X"):
radix = 16
if (strLength >= 3) and (s[0:3] == "-0x"):
radix = 16
if (strLength >= 3) and (s[0:3] == "-0X"):
radix = 16
return int(s, radix)
# -------------------------------------------------------------------
def readAllInts(self):
"""Read all remaining strings from the stream wrapped by self, convert
each to an int, and return those ints in an array.
Raise a ValueError if any of the strings cannot be converted to
an int.
"""
strings = self.readAllStrings()
ints = []
for s in strings:
i = int(s)
ints.append(i)
return ints
# -------------------------------------------------------------------
def readFloat(self):
"""Discard leading white space characters from the stream wrapped by
self.
Then read from the stream a sequence of characters comprising a
float. Convert the sequence of characters to an float, and
return the float. Raise an EOFError if no non-whitespace
characters remain in the stream. Raise a ValueError if the next
characters to be read from the stream cannot comprise a float.
"""
s = self._readRegExp(r"[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?")
return float(s)
# -------------------------------------------------------------------
def readAllFloats(self):
"""Read all remaining strings from the stream wrapped by self, convert
each to a float, and return those floats in an array.
Raise a ValueError if any of the strings cannot be converted to
a float.
"""
strings = self.readAllStrings()
floats = []
for s in strings:
f = float(s)
floats.append(f)
return floats
# -------------------------------------------------------------------
def readBool(self):
"""Discard leading white space characters from the stream wrapped by
self.
Then read from the stream a sequence of characters comprising a
bool. Convert the sequence of characters to an bool, and return
the bool. Raise an EOFError if no non-whitespace characters
remain in the stream. Raise a ValueError if the next characters
to be read from the stream cannot comprise an bool.
"""
s = self._readRegExp(r"(True)|(False)|1|0")
if (s == "True") or (s == "1"):
return True
return False
# -----------------------------------------------------------------------
def readAllBools(self):
"""Read all remaining strings from the stream wrapped by self, convert
each to a bool, and return those bools in an array.
Raise a ValueError if any of the strings cannot be converted to
a bool.
"""
strings = self.readAllStrings()
bools = []
for s in strings:
b = bool(s)
bools.append(b)
return bools
# -------------------------------------------------------------------
def readString(self):
"""Discard leading white space characters from the stream wrapped by
self.
Then read from the stream a sequence of characters comprising a
string, and return the string. Raise an EOFError if no non-
whitespace characters remain in the stream.
"""
s = self._readRegExp(r"\S+")
return s
# -----------------------------------------------------------------------
def readAllStrings(self):
"""Read all remaining strings from the stream wrapped by self, and
return them in an array."""
strings = []
while not self.isEmpty():
s = self.readString()
strings.append(s)
return strings
# -------------------------------------------------------------------
def hasNextLine(self):
"""Return True iff the stream wrapped by self has a next line."""
if self._buffer != "":
return True
else:
self._buffer = self._stream.readline()
if sys.hexversion < 0x03000000 or self._readingWebPage:
self._buffer = self._buffer.decode("utf-8")
if self._buffer == "":
return False
return True
# -------------------------------------------------------------------
def readLine(self):
"""Read and return as a string the next line of the stream wrapped by
self.
Raise an EOFError is there is no next line.
"""
if not self.hasNextLine():
raise EOFError()
s = self._buffer
self._buffer = ""
return s.rstrip("\n")
# -------------------------------------------------------------------
def readAllLines(self):
"""Read all remaining lines from the stream wrapped by self, and return
them as strings in an array."""
lines = []
while self.hasNextLine():
line = self.readLine()
lines.append(line)
return lines
# -------------------------------------------------------------------
def readAll(self):
"""Read and return as a string all remaining lines of the stream
wrapped by self."""
s = self._buffer
self._buffer = ""
for line in self._stream:
if sys.hexversion < 0x03000000 or self._readingWebPage:
line = line.decode("utf-8")
s += line
return s
# -------------------------------------------------------------------
def __del__(self):
"""Close the stream wrapped by self."""
if self._stream is not None:
self._stream.close()
# =======================================================================
# For Testing
# =======================================================================
def _main():
"""For testing.
The first command-line argument should be the name of the method
that should be called. The optional second command-line argument
should be the file or URL to read.
"""
from itu.algs4.stdlib import stdio
testId = sys.argv[1]
if len(sys.argv) > 2:
inStream = InStream(sys.argv[2])
else:
inStream = InStream()
if testId == "readInt":
stdio.writeln(inStream.readInt())
elif testId == "readAllInts":
stdio.writeln(inStream.readAllInts())
elif testId == "readFloat":
stdio.writeln(inStream.readFloat())
elif testId == "readAllFloats":
stdio.writeln(inStream.readAllFloats())
elif testId == "readBool":
stdio.writeln(inStream.readBool())
elif testId == "readAllBools":
stdio.writeln(inStream.readAllBools())
elif testId == "readString":
stdio.writeln(inStream.readString())
elif testId == "readAllStrings":
stdio.writeln(inStream.readAllStrings())
elif testId == "readLine":
stdio.writeln(inStream.readLine())
elif testId == "readAllLines":
stdio.writeln(inStream.readAllLines())
elif testId == "readAll":
stdio.writeln(inStream.readAll())
if __name__ == "__main__":
_main()
================================================
FILE: itu/algs4/stdlib/outstream.py
================================================
# code based on https://introcs.cs.princeton.edu/python/code/stdlib-python.zip as downloaded in dec 2017
"""outstream.py.
The outstream module defines the OutStream class.
"""
import sys
# -----------------------------------------------------------------------
class OutStream:
"""An OutStream object wraps around a text file or sys.stdout, and supports
writing to that stream."""
# -------------------------------------------------------------------
def __init__(self, f=None):
"""Construct self to wrap around a stream.
If f is provided, then the stream is a file of that name.
Otherwise the stream is standard output.
"""
if f is None:
self._stream = sys.stdout
else:
if sys.hexversion < 0x03000000:
self._stream = open(f, "w")
else:
self._stream = open(f, "w", encoding="utf-8")
# -------------------------------------------------------------------
def writeln(self, x=""):
"""Write x and an end-of-line mark to the stream wrapped by self."""
if sys.hexversion < 0x03000000:
print("Error: Python 3 is required.", file=sys.stderr)
sys.exit(1)
# x = unicode(x)
# x = x.encode("utf-8")
else:
x = str(x)
self._stream.write(x)
self._stream.write("\n")
self._stream.flush()
# -------------------------------------------------------------------
def write(self, x=""):
"""Write x to the stream wrapped by self."""
if sys.hexversion < 0x03000000:
print("Error: Python 3 is required.", file=sys.stderr)
sys.exit(1)
# x = unicode(x)
# x = x.encode("utf-8")
else:
x = str(x)
self._stream.write(x)
self._stream.flush()
# -------------------------------------------------------------------
def writef(self, fmt, *args):
"""Write each element of args to the stream wrapped by self.
Use the format specified by string fmt.
"""
x = fmt % args
if sys.hexversion < 0x03000000:
print("Error: Python 3 is required.", file=sys.stderr)
sys.exit(1)
# x = unicode(x)
# x = x.encode("utf-8")
self._stream.write(x)
self._stream.flush()
# -------------------------------------------------------------------
def __del__(self):
"""Close the stream wrapped by self."""
self._stream.close()
================================================
FILE: itu/algs4/stdlib/picture.py
================================================
# code based on https://introcs.cs.princeton.edu/python/code/stdlib-python.zip as downloaded in dec 2017
"""picture.py.
The picture module defines the Picture class.
"""
# -----------------------------------------------------------------------
import pygame
from itu.algs4.stdlib import color as color
# -----------------------------------------------------------------------
_DEFAULT_WIDTH = 512
_DEFAULT_HEIGHT = 512
# -----------------------------------------------------------------------
class Picture:
"""A Picture object models an image.
It is initialized such that it has a given width and height and
contains all black pixels. Subsequently you can load an image from a
given JPG or PNG file.
"""
def __init__(self, arg1=None, arg2=None):
"""If both arg1 and arg2 are None, then construct self such that it is
all black with _DEFAULT_WIDTH and height _DEFAULT_HEIGHT.
If arg1 is not None and arg2 is None, then construct self by
reading from the file whose name is arg1. If neither arg1 nor
arg2 is None, then construct self such that it is all black with
width arg1 and and height arg2.
"""
if (arg1 is None) and (arg2 is None):
maxW = _DEFAULT_WIDTH
maxH = _DEFAULT_HEIGHT
self._surface = pygame.Surface((maxW, maxH))
self._surface.fill((0, 0, 0))
elif (arg1 is not None) and (arg2 is None):
fileName = arg1
try:
self._surface = pygame.image.load(fileName)
except pygame.error:
raise IOError()
elif (arg1 is not None) and (arg2 is not None):
maxW = arg1
maxH = arg2
self._surface = pygame.Surface((maxW, maxH))
self._surface.fill((0, 0, 0))
else:
raise ValueError()
# -------------------------------------------------------------------
# def load(self, f):
# """
# Change self by reading from the file whose name is f. The
# dimensions of the read image override the dimensions specified
# in the constructor.
# """
# if sys.hexversion >= 0x03000000:
# # Hack because Pygame without full image support
# # can handle only .bmp files.
# bmpFileName = f + '.bmp'
# os.system('convert ' + f + ' ' + bmpFileName)
# self._surface = pygame.image.load(bmpFileName)
# os.system('rm ' + bmpFileName)
# else:
# self._surface = pygame.image.load(f)
# self._surface = pygame.image.load(f)
# -------------------------------------------------------------------
def save(self, f):
"""Save self to the file whose name is f."""
# if sys.hexversion >= 0x03000000:
# # Hack because Pygame without full image support
# # can handle only .bmp files.
# bmpFileName = f + '.bmp'
# pygame.image.save(self._surface, bmpFileName)
# os.system('convert ' + bmpFileName + ' ' + f)
# os.system('rm ' + bmpFileName)
# else:
# pygame.image.save(self._surface, f)
pygame.image.save(self._surface, f)
# -------------------------------------------------------------------
def width(self):
"""Return the width of self."""
return self._surface.get_width()
# -------------------------------------------------------------------
def height(self):
"""Return the height of self."""
return self._surface.get_height()
# -------------------------------------------------------------------
def get(self, x, y):
"""Return the color of self at location (x, y)."""
pygameColor = self._surface.get_at((x, y))
return color.Color(pygameColor.r, pygameColor.g, pygameColor.b)
# -------------------------------------------------------------------
def set(self, x, y, c):
"""Set the color of self at location (x, y) to c."""
pygameColor = pygame.Color(c.getRed(), c.getGreen(), c.getBlue(), 0)
self._surface.set_at((x, y), pygameColor)
================================================
FILE: itu/algs4/stdlib/stdarray.py
================================================
# code based on https://introcs.cs.princeton.edu/python/code/stdlib-python.zip as downloaded in dec 2017
"""stdarray.py.
The stdarray module defines functions related to creating, reading, and
writing one- and two-dimensional arrays.
"""
from itu.algs4.stdlib import stdio
# =======================================================================
# Array creation functions
# =======================================================================
def create1D(length, value=None):
"""Create and return a 1D array containing length elements, each
initialized to value."""
return [value] * length
# -----------------------------------------------------------------------
def create2D(rowCount, colCount, value=None):
"""Create and return a 2D array having rowCount rows and colCount columns,
with each element initialized to value."""
a = [None] * rowCount
for row in range(rowCount):
a[row] = [value] * colCount
return a
# =======================================================================
# Array writing functions
# =======================================================================
def write1D(a):
"""Write array a to sys.stdout.
First write its length. bool objects are written as 0 and 1, not
False and True.
"""
length = len(a)
stdio.writeln(length)
for i in range(length):
# stdio.writef('%9.5f ', a[i])
element = a[i]
if isinstance(element, bool):
if element:
stdio.write(1)
else:
stdio.write(0)
else:
stdio.write(element)
stdio.write(" ")
stdio.writeln()
# -----------------------------------------------------------------------
def write2D(a):
"""Write two-dimensional array a to sys.stdout.
First write its dimensions. bool objects are written as 0 and 1, not
False and True.
"""
rowCount = len(a)
colCount = len(a[0])
stdio.writeln(str(rowCount) + " " + str(colCount))
for row in range(rowCount):
for col in range(colCount):
# stdio.writef('%9.5f ', a[row][col])
element = a[row][col]
if isinstance(element, bool):
if element:
stdio.write(1)
else:
stdio.write(0)
else:
stdio.write(element)
stdio.write(" ")
stdio.writeln()
# =======================================================================
# Array reading functions
# =======================================================================
def readInt1D():
"""Read from sys.stdin and return an array of integers.
An integer at the beginning of sys.stdin defines the array's length.
"""
count = stdio.readInt()
a = create1D(count, None)
for i in range(count):
a[i] = stdio.readInt()
return a
# -----------------------------------------------------------------------
def readInt2D():
"""Read from sys.stdin and return a two-dimensional array of integers.
Two integers at the beginning of sys.stdin define the array's
dimensions.
"""
rowCount = stdio.readInt()
colCount = stdio.readInt()
a = create2D(rowCount, colCount, 0)
for row in range(rowCount):
for col in range(colCount):
a[row][col] = stdio.readInt()
return a
# -----------------------------------------------------------------------
def readFloat1D():
"""Read from sys.stdin and return an array of floats.
An integer at the beginning of sys.stdin defines the array's length.
"""
count = stdio.readInt()
a = create1D(count, None)
for i in range(count):
a[i] = stdio.readFloat()
return a
# -----------------------------------------------------------------------
def readFloat2D():
"""Read from sys.stdin and return a two-dimensional array of floats.
Two integers at the beginning of sys.stdin define the array's
dimensions.
"""
rowCount = stdio.readInt()
colCount = stdio.readInt()
a = create2D(rowCount, colCount, 0.0)
for row in range(rowCount):
for col in range(colCount):
a[row][col] = stdio.readFloat()
return a
# -----------------------------------------------------------------------
def readBool1D():
"""Read from sys.stdin and return an array of booleans.
An integer at the beginning of sys.stdin defines the array's length.
"""
count = stdio.readInt()
a = create1D(count, None)
for i in range(count):
a[i] = stdio.readBool()
return a
# -----------------------------------------------------------------------
def readBool2D():
"""Read from sys.stdin and return a two-dimensional array of booleans.
Two integers at the beginning of sys.stdin define the array's
dimensions.
"""
rowCount = stdio.readInt()
colCount = stdio.readInt()
a = create2D(rowCount, colCount, False)
for row in range(rowCount):
for col in range(colCount):
a[row][col] = stdio.readBool()
return a
# =======================================================================
def _main():
"""For testing."""
write2D(readFloat2D())
write2D(readBool2D())
if __name__ == "__main__":
_main()
================================================
FILE: itu/algs4/stdlib/stdaudio.py
================================================
# code based on https://introcs.cs.princeton.edu/python/code/stdlib-python.zip as downloaded in dec 2017
"""stdaudio.py.
The stdaudio module defines functions related to audio.
"""
# -----------------------------------------------------------------------
import sys
import numpy
import pygame
from itu.algs4.stdlib import stdio as stdio
# -----------------------------------------------------------------------
_SAMPLES_PER_SECOND = 44100
_SAMPLE_SIZE = -16 # Each sample is a signed 16-bit int
_CHANNEL_COUNT = 1 # 1 => mono, 2 => stereo
_AUDIO_BUFFER_SIZE = 1024 # In number of samples
_CHECK_RATE = 44100 # How often to check the queue
_myBuffer = []
_MY_BUFFER_MAX_LENGTH = 4096 # Determined experimentally.
def wait():
"""Wait for the sound queue to become empty.
Informally, wait for the currently playing sound to finish.
"""
# Can have at most one sound in the queue. So must wait for the
# queue to become empty before adding a new sound to the queue.
clock = pygame.time.Clock()
while _channel.get_queue() is not None:
# while pygame.mixer.get_busy():
clock.tick(_CHECK_RATE)
def playSample(s):
"""Play sound sample s."""
global _myBuffer
_myBuffer.append(s)
if len(_myBuffer) > _MY_BUFFER_MAX_LENGTH:
temp = []
for sample in _myBuffer:
temp.append(numpy.int16(sample * float(0x7FFF)))
samples = numpy.array(temp, numpy.int16)
sound = pygame.sndarray.make_sound(samples)
wait()
_channel.queue(sound)
_myBuffer = []
def playSamples(a):
"""Play all sound samples in array a."""
for sample in a:
playSample(sample)
def playArray(a):
"""This function is deprecated.
It has the same behavior as stdaudio.playSamples(). Please call
stdaudio.playSamples() instead.
"""
playSamples(a)
def playFile(f):
"""Play all sound samples in the file whose name is f.wav."""
a = read(f)
playSamples(a)
# sound = pygame.mixer.Sound(fileName)
# samples = pygame.sndarray.samples(sound)
# wait()
# sound.play()
def save(f, a):
"""Save all samples in array a to the WAVE file whose name is f.wav."""
# Saving to a WAV file isn't handled by PyGame, so use the
# standard "wave" module instead.
import wave
fileName = f + ".wav"
temp = []
for sample in a:
temp.append(int(sample * float(0x7FFF)))
samples = numpy.array(temp, numpy.int16)
file = wave.open(fileName, "w")
file.setnchannels(_CHANNEL_COUNT)
file.setsampwidth(2) # 2 bytes
file.setframerate(_SAMPLES_PER_SECOND)
file.setnframes(len(samples))
file.setcomptype("NONE", "descrip") # No compression
file.writeframes(samples.tostring())
file.close()
def read(f):
"""Read all samples from the WAVE file whose names is f.wav.
Store the samples in an array, and return the array.
"""
fileName = f + ".wav"
sound = pygame.mixer.Sound(fileName)
samples = pygame.sndarray.samples(sound)
temp = []
for i in range(len(samples)):
temp.append(float(samples[i]) / float(0x7FFF))
return temp
# Initialize PyGame to handle audio.
try:
pygame.mixer.init(
_SAMPLES_PER_SECOND, _SAMPLE_SIZE, _CHANNEL_COUNT, _AUDIO_BUFFER_SIZE
)
_channel = pygame.mixer.Channel(0)
except pygame.error:
stdio.writeln("Could not initialize PyGame")
sys.exit(1)
# -----------------------------------------------------------------------
def _createTextAudioFile():
"""For testing.
Create a text audio file.
"""
notes = [
7,
0.270,
5,
0.090,
3,
0.180,
5,
0.180,
7,
0.180,
6,
0.180,
7,
0.180,
3,
0.180,
5,
0.180,
5,
0.180,
5,
0.180,
5,
0.900,
5,
0.325,
3,
0.125,
2,
0.180,
3,
0.180,
5,
0.180,
4,
0.180,
5,
0.180,
2,
0.180,
3,
0.180,
3,
0.180,
3,
0.180,
3,
0.900,
]
from itu.algs4.stdlib import outstream
outStream = outstream.OutStream("looney.txt")
for note in notes:
outStream.writeln(note)
def _main():
"""For testing."""
import math
import os
from itu.algs4.stdlib import instream, stdio
_createTextAudioFile()
stdio.writeln("Creating and playing in small chunks...")
sps = _SAMPLES_PER_SECOND
inStream = instream.InStream("looney.txt")
while not inStream.isEmpty():
pitch = inStream.readInt()
duration = inStream.readFloat()
hz = 440 * math.pow(2, pitch / 12.0)
N = int(sps * duration)
notes = []
for i in range(N + 1):
notes.append(math.sin(2 * math.pi * i * hz / sps))
playSamples(notes)
wait()
stdio.writeln("Creating and playing in one large chunk...")
sps = _SAMPLES_PER_SECOND
notes = []
inStream = instream.InStream("looney.txt")
while not inStream.isEmpty():
pitch = inStream.readInt()
duration = inStream.readFloat()
hz = 440 * math.pow(2, pitch / 12.0)
N = int(sps * duration)
for i in range(N + 1):
notes.append(math.sin(2 * math.pi * i * hz / sps))
playSamples(notes)
wait()
stdio.writeln("Saving...")
save("looney", notes)
stdio.writeln("Reading...")
notes = read("looney")
stdio.writeln("Playing an array...")
playSamples(notes)
wait()
stdio.writeln("Playing a file...")
playFile("looney")
wait()
os.remove("looney.wav")
os.remove("looney.txt")
if __name__ == "__main__":
_main()
================================================
FILE: itu/algs4/stdlib/stddraw.py
================================================
# code based on https://introcs.cs.princeton.edu/python/code/stdlib-python.zip as downloaded in dec 2017
"""stddraw.py.
The stddraw module defines functions that allow the user to create a
drawing. A drawing appears on the canvas. The canvas appears in the
window. As a convenience, the module also imports the commonly used
Color objects defined in the color module.
"""
import os
import sys
import time
import pygame
import pygame.font
import pygame.gfxdraw
from itu.algs4.stdlib.color import (
BLACK,
BLUE,
CYAN,
DARK_BLUE,
DARK_GREEN,
DARK_RED,
GREEN,
MAGENTA,
ORANGE,
PINK,
RED,
WHITE,
YELLOW,
)
if sys.hexversion < 0x03000000:
import tkFileDialog
import Tkinter
import tkMessageBox
else:
import tkinter as Tkinter
from tkinter import filedialog as tkFileDialog
from tkinter import messagebox as tkMessageBox
# -----------------------------------------------------------------------
# Define colors so clients need not import the color module.
# -----------------------------------------------------------------------
# Default Sizes and Values
_BORDER = 0.0
# _BORDER = 0.05
_DEFAULT_XMIN = 0.0
_DEFAULT_XMAX = 1.0
_DEFAULT_YMIN = 0.0
_DEFAULT_YMAX = 1.0
_DEFAULT_CANVAS_SIZE = 512
_DEFAULT_PEN_RADIUS = 0.005 # Maybe change this to 0.0 in the future.
_DEFAULT_PEN_COLOR = BLACK
_DEFAULT_FONT_FAMILY = "Helvetica"
_DEFAULT_FONT_SIZE = 12
_xmin = None
_ymin = None
_xmax = None
_ymax = None
_fontFamily = _DEFAULT_FONT_FAMILY
_fontSize = _DEFAULT_FONT_SIZE
_canvasWidth = float(_DEFAULT_CANVAS_SIZE)
_canvasHeight = float(_DEFAULT_CANVAS_SIZE)
_penRadius = None
_penColor = _DEFAULT_PEN_COLOR
_keysTyped = []
# Has the window been created?
_windowCreated = False
_surface = None
_background = None
# -----------------------------------------------------------------------
# Begin added by Alan J. Broder
# -----------------------------------------------------------------------
# Keep track of mouse status
# Has the mouse been left-clicked since the last time we checked?
_mousePressed = False
# The position of the mouse as of the most recent mouse click
_mousePos = None
# -----------------------------------------------------------------------
# End added by Alan J. Broder
# -----------------------------------------------------------------------
# -----------------------------------------------------------------------
def _pygameColor(c):
"""Convert c, an object of type color.Color, to an equivalent object of
type pygame.Color.
Return the result.
"""
r = c.getRed()
g = c.getGreen()
b = c.getBlue()
return pygame.Color(r, g, b)
# -----------------------------------------------------------------------
# Private functions to scale and factor X and Y values.
def _scaleX(x):
return _canvasWidth * (x - _xmin) / (_xmax - _xmin)
def _scaleY(y):
return _canvasHeight * (_ymax - y) / (_ymax - _ymin)
def _factorX(w):
return w * _canvasWidth / abs(_xmax - _xmin)
def _factorY(h):
return h * _canvasHeight / abs(_ymax - _ymin)
# -----------------------------------------------------------------------
# Begin added by Alan J. Broder
# -----------------------------------------------------------------------
def _userX(x):
return _xmin + x * (_xmax - _xmin) / _canvasWidth
def _userY(y):
return _ymax - y * (_ymax - _ymin) / _canvasHeight
# -----------------------------------------------------------------------
# End added by Alan J. Broder
# -----------------------------------------------------------------------
# -----------------------------------------------------------------------
def setCanvasSize(w=_DEFAULT_CANVAS_SIZE, h=_DEFAULT_CANVAS_SIZE):
"""Set the size of the canvas to w pixels wide and h pixels high.
Calling this function is optional. If you call it, you must do so
before calling any drawing function.
"""
global _background
global _surface
global _canvasWidth
global _canvasHeight
global _windowCreated
if _windowCreated:
raise Exception("The stddraw window already was created")
if (w < 1) or (h < 1):
raise Exception("width and height must be positive")
_canvasWidth = w
_canvasHeight = h
_background = pygame.display.set_mode([w, h])
pygame.display.set_caption("stddraw window (r-click to save)")
_surface = pygame.Surface((w, h))
_surface.fill(_pygameColor(WHITE))
_windowCreated = True
def setXscale(min=_DEFAULT_XMIN, max=_DEFAULT_XMAX):
"""Set the x-scale of the canvas such that the minimum x value is min and
the maximum x value is max."""
global _xmin
global _xmax
min = float(min)
max = float(max)
if min >= max:
raise Exception("min must be less than max")
size = max - min
_xmin = min - _BORDER * size
_xmax = max + _BORDER * size
def setYscale(min=_DEFAULT_YMIN, max=_DEFAULT_YMAX):
"""Set the y-scale of the canvas such that the minimum y value is min and
the maximum y value is max."""
global _ymin
global _ymax
min = float(min)
max = float(max)
if min >= max:
raise Exception("min must be less than max")
size = max - min
_ymin = min - _BORDER * size
_ymax = max + _BORDER * size
def setPenRadius(r=_DEFAULT_PEN_RADIUS):
"""Set the pen radius to r, thus affecting the subsequent drawing of points
and lines.
If r is 0.0, then points will be drawn with the minimum possible
radius and lines with the minimum possible width.
"""
global _penRadius
r = float(r)
if r < 0.0:
raise Exception("Argument to setPenRadius() must be non-neg")
_penRadius = r * float(_DEFAULT_CANVAS_SIZE)
def setPenColor(c=_DEFAULT_PEN_COLOR):
"""Set the pen color to c, where c is an object of class color.Color.
c defaults to stddraw.BLACK.
"""
global _penColor
_penColor = c
def setFontFamily(f=_DEFAULT_FONT_FAMILY):
"""Set the font family to f (e.g. 'Helvetica' or 'Courier')."""
global _fontFamily
_fontFamily = f
def setFontSize(s=_DEFAULT_FONT_SIZE):
"""Set the font size to s (e.g. 12 or 16)."""
global _fontSize
_fontSize = s
# -----------------------------------------------------------------------
def _makeSureWindowCreated():
global _windowCreated
if not _windowCreated:
setCanvasSize()
_windowCreated = True
# -----------------------------------------------------------------------
# Functions to draw shapes, text, and images on the background canvas.
def _pixel(x, y):
"""Draw on the background canvas a pixel at (x, y)."""
_makeSureWindowCreated()
xs = _scaleX(x)
xy = _scaleY(y)
pygame.gfxdraw.pixel(
_surface, int(round(xs)), int(round(xy)), _pygameColor(_penColor)
)
def point(x, y):
"""Draw on the background canvas a point at (x, y)."""
_makeSureWindowCreated()
x = float(x)
y = float(y)
# If the radius is too small, then simply draw a pixel.
if _penRadius <= 1.0:
_pixel(x, y)
else:
xs = _scaleX(x)
ys = _scaleY(y)
pygame.draw.ellipse(
_surface,
_pygameColor(_penColor),
pygame.Rect(
xs - _penRadius, ys - _penRadius, _penRadius * 2.0, _penRadius * 2.0
),
0,
)
def _thickLine(x0, y0, x1, y1, r):
"""Draw on the background canvas a line from (x0, y0) to (x1, y1).
Draw the line with a pen whose radius is r.
"""
xs0 = _scaleX(x0)
ys0 = _scaleY(y0)
xs1 = _scaleX(x1)
ys1 = _scaleY(y1)
if (abs(xs0 - xs1) < 1.0) and (abs(ys0 - ys1) < 1.0):
filledCircle(x0, y0, r)
return
xMid = (x0 + x1) / 2
yMid = (y0 + y1) / 2
_thickLine(x0, y0, xMid, yMid, r)
_thickLine(xMid, yMid, x1, y1, r)
def line(x0, y0, x1, y1):
"""Draw on the background canvas a line from (x0, y0) to (x1, y1)."""
THICK_LINE_CUTOFF = 3 # pixels
_makeSureWindowCreated()
x0 = float(x0)
y0 = float(y0)
x1 = float(x1)
y1 = float(y1)
lineWidth = _penRadius * 2.0
if lineWidth == 0.0:
lineWidth = 1.0
if lineWidth < THICK_LINE_CUTOFF:
x0s = _scaleX(x0)
y0s = _scaleY(y0)
x1s = _scaleX(x1)
y1s = _scaleY(y1)
pygame.draw.line(
_surface,
_pygameColor(_penColor),
(x0s, y0s),
(x1s, y1s),
int(round(lineWidth)),
)
else:
_thickLine(x0, y0, x1, y1, _penRadius / _DEFAULT_CANVAS_SIZE)
def circle(x, y, r):
"""Draw on the background canvas a circle of radius r centered on (x,
y)."""
_makeSureWindowCreated()
x = float(x)
y = float(y)
r = float(r)
ws = _factorX(2.0 * r)
hs = _factorY(2.0 * r)
# If the radius is too small, then simply draw a pixel.
if (ws <= 1.0) and (hs <= 1.0):
_pixel(x, y)
else:
xs = _scaleX(x)
ys = _scaleY(y)
pygame.draw.ellipse(
_surface,
_pygameColor(_penColor),
pygame.Rect(xs - ws / 2.0, ys - hs / 2.0, ws, hs),
int(round(_penRadius)),
)
def filledCircle(x, y, r):
"""Draw on the background canvas a filled circle of radius r centered on
(x, y)."""
_makeSureWindowCreated()
x = float(x)
y = float(y)
r = float(r)
ws = _factorX(2.0 * r)
hs = _factorY(2.0 * r)
# If the radius is too small, then simply draw a pixel.
if (ws <= 1.0) and (hs <= 1.0):
_pixel(x, y)
else:
xs = _scaleX(x)
ys = _scaleY(y)
pygame.draw.ellipse(
_surface,
_pygameColor(_penColor),
pygame.Rect(xs - ws / 2.0, ys - hs / 2.0, ws, hs),
0,
)
def rectangle(x, y, w, h):
"""Draw on the background canvas a rectangle of width w and height h whose
lower left point is (x, y)."""
_makeSureWindowCreated()
x = float(x)
y = float(y)
w = float(w)
h = float(h)
ws = _factorX(w)
hs = _factorY(h)
# If the rectangle is too small, then simply draw a pixel.
if (ws <= 1.0) and (hs <= 1.0):
_pixel(x, y)
else:
xs = _scaleX(x)
ys = _scaleY(y)
pygame.draw.rect(
_surface,
_pygameColor(_penColor),
pygame.Rect(xs, ys - hs, ws, hs),
int(round(_penRadius)),
)
def filledRectangle(x, y, w, h):
"""Draw on the background canvas a filled rectangle of width w and height h
whose lower left point is (x, y)."""
_makeSureWindowCreated()
x = float(x)
y = float(y)
w = float(w)
h = float(h)
ws = _factorX(w)
hs = _factorY(h)
# If the rectangle is too small, then simply draw a pixel.
if (ws <= 1.0) and (hs <= 1.0):
_pixel(x, y)
else:
xs = _scaleX(x)
ys = _scaleY(y)
pygame.draw.rect(
_surface, _pygameColor(_penColor), pygame.Rect(xs, ys - hs, ws, hs), 0
)
def square(x, y, r):
"""Draw on the background canvas a square whose sides are of length 2r,
centered on (x, y)."""
_makeSureWindowCreated()
rectangle(x - r, y - r, 2.0 * r, 2.0 * r)
def filledSquare(x, y, r):
"""Draw on the background canvas a filled square whose sides are of length
2r, centered on (x, y)."""
_makeSureWindowCreated()
filledRectangle(x - r, y - r, 2.0 * r, 2.0 * r)
def polygon(x, y):
"""Draw on the background canvas a polygon with coordinates (x[i],
y[i])."""
_makeSureWindowCreated()
# Scale X and Y values.
xScaled = []
for xi in x:
xScaled.append(_scaleX(float(xi)))
yScaled = []
for yi in y:
yScaled.append(_scaleY(float(yi)))
points = []
for i in range(len(x)):
points.append((xScaled[i], yScaled[i]))
points.append((xScaled[0], yScaled[0]))
pygame.draw.polygon(
_surface, _pygameColor(_penColor), points, int(round(_penRadius))
)
def filledPolygon(x, y):
"""Draw on the background canvas a filled polygon with coordinates (x[i],
y[i])."""
_makeSureWindowCreated()
# Scale X and Y values.
xScaled = []
for xi in x:
xScaled.append(_scaleX(float(xi)))
yScaled = []
for yi in y:
yScaled.append(_scaleY(float(yi)))
points = []
for i in range(len(x)):
points.append((xScaled[i], yScaled[i]))
points.append((xScaled[0], yScaled[0]))
pygame.draw.polygon(_surface, _pygameColor(_penColor), points, 0)
def text(x, y, s):
"""Draw string s on the background canvas centered at (x, y)."""
_makeSureWindowCreated()
x = float(x)
y = float(y)
xs = _scaleX(x)
ys = _scaleY(y)
font = pygame.font.SysFont(_fontFamily, _fontSize)
text = font.render(s, 1, _pygameColor(_penColor))
textpos = text.get_rect(center=(xs, ys))
_surface.blit(text, textpos)
def picture(pic, x=None, y=None):
"""Draw pic on the background canvas centered at (x, y).
pic is an object of class picture.Picture. x and y default to the
midpoint of the background canvas.
"""
_makeSureWindowCreated()
# By default, draw pic at the middle of the surface.
if x is None:
x = (_xmax + _xmin) / 2.0
if y is None:
y = (_ymax + _ymin) / 2.0
x = float(x)
y = float(y)
xs = _scaleX(x)
ys = _scaleY(y)
ws = pic.width()
hs = pic.height()
picSurface = pic._surface # violates encapsulation
_surface.blit(picSurface, [xs - ws / 2.0, ys - hs / 2.0, ws, hs])
def clear(c=WHITE):
"""Clear the background canvas to color c, where c is an object of class
color.Color.
c defaults to stddraw.WHITE.
"""
_makeSureWindowCreated()
_surface.fill(_pygameColor(c))
def save(f):
"""Save the window canvas to file f."""
_makeSureWindowCreated()
# if sys.hexversion >= 0x03000000:
# # Hack because Pygame without full image support
# # can handle only .bmp files.
# bmpFileName = f + '.bmp'
# pygame.image.save(_surface, bmpFileName)
# os.system('convert ' + bmpFileName + ' ' + f)
# os.system('rm ' + bmpFileName)
# else:
# pygame.image.save(_surface, f)
pygame.image.save(_surface, f)
# -----------------------------------------------------------------------
def _show():
"""Copy the background canvas to the window canvas."""
_background.blit(_surface, (0, 0))
pygame.display.flip()
_checkForEvents()
def _showAndWaitForever():
"""Copy the background canvas to the window canvas.
Then wait forever, that is, until the user closes the stddraw
window.
"""
_makeSureWindowCreated()
_show()
QUANTUM = 0.1
while True:
time.sleep(QUANTUM)
_checkForEvents()
def show(msec=float("inf")):
"""Copy the background canvas to the window canvas, and then wait for msec
milliseconds.
msec defaults to infinity.
"""
if msec == float("inf"):
_showAndWaitForever()
_makeSureWindowCreated()
_show()
_checkForEvents()
# Sleep for the required time, but check for events every
# QUANTUM seconds.
QUANTUM = 0.1
sec = msec / 1000.0
if sec < QUANTUM:
time.sleep(sec)
return
secondsWaited = 0.0
while secondsWaited < sec:
time.sleep(QUANTUM)
secondsWaited += QUANTUM
_checkForEvents()
# -----------------------------------------------------------------------
def _saveToFile():
"""Display a dialog box that asks the user for a file name.
Save the drawing to the specified file. Display a confirmation
dialog box if successful, and an error dialog box otherwise. The
dialog boxes are displayed using Tkinter, which (on some computers)
is incompatible with Pygame. So the dialog boxes must be displayed
from child processes.
"""
import subprocess
_makeSureWindowCreated()
stddrawPath = os.path.realpath(__file__)
childProcess = subprocess.Popen(
[sys.executable, stddrawPath, "getFileName"], stdout=subprocess.PIPE
)
so, _ = childProcess.communicate()
fileName = so.strip()
if sys.hexversion >= 0x03000000:
fileName = fileName.decode("utf-8")
if fileName == "":
return
if not fileName.endswith((".jpg", ".png")):
childProcess = subprocess.Popen(
[
sys.executable,
stddrawPath,
"reportFileSaveError",
'File name must end with ".jpg" or ".png".',
]
)
return
try:
save(fileName)
childProcess = subprocess.Popen(
[sys.executable, stddrawPath, "confirmFileSave"]
)
except (pygame.error) as e:
childProcess = subprocess.Popen(
[sys.executable, stddrawPath, "reportFileSaveError", str(e)]
)
def _checkForEvents():
"""Check if any new event has occured (such as a key typed or button
pressed).
If a key has been typed, then put that key in a queue.
"""
global _keysTyped
# -------------------------------------------------------------------
# Begin added by Alan J. Broder
# -------------------------------------------------------------------
global _mousePos
global _mousePressed
# -------------------------------------------------------------------
# End added by Alan J. Broder
# -------------------------------------------------------------------
_makeSureWindowCreated()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
_keysTyped = [event.unicode] + _keysTyped
elif (event.type == pygame.MOUSEBUTTONUP) and (event.button == 3):
_saveToFile()
# ---------------------------------------------------------------
# Begin added by Alan J. Broder
# ---------------------------------------------------------------
# Every time the mouse button is pressed, remember
# the mouse position as of that press.
elif (event.type == pygame.MOUSEBUTTONDOWN) and (event.button == 1):
_mousePressed = True
_mousePos = event.pos
# ---------------------------------------------------------------
# End added by Alan J. Broder
# ---------------------------------------------------------------
# -----------------------------------------------------------------------
# Functions for retrieving keys
def hasNextKeyTyped():
"""Return True if the queue of keys the user typed is not empty.
Otherwise return False.
"""
return _keysTyped != []
def nextKeyTyped():
"""Remove the first key from the queue of keys that the the user typed, and
return that key."""
return _keysTyped.pop()
# -----------------------------------------------------------------------
# Begin added by Alan J. Broder
# -----------------------------------------------------------------------
# Functions for dealing with mouse clicks
def mousePressed():
"""Return True if the mouse has been left-clicked since the last time
mousePressed was called, and False otherwise."""
global _mousePressed
if _mousePressed:
_mousePressed = False
return True
return False
def mouseX():
"""Return the x coordinate in user space of the location at which the mouse
was most recently left-clicked.
If a left-click hasn't happened yet, raise an exception, since
mouseX() shouldn't be called until mousePressed() returns True.
"""
if _mousePos:
return _userX(_mousePos[0])
raise Exception("Can't determine mouse position if a click hasn't happened")
def mouseY():
"""Return the y coordinate in user space of the location at which the mouse
was most recently left-clicked.
If a left-click hasn't happened yet, raise an exception, since
mouseY() shouldn't be called until mousePressed() returns True.
"""
if _mousePos:
return _userY(_mousePos[1])
raise Exception("Can't determine mouse position if a click hasn't happened")
# -----------------------------------------------------------------------
# End added by Alan J. Broder
# -----------------------------------------------------------------------
# -----------------------------------------------------------------------
# Initialize the x scale, the y scale, and the pen radius.
setXscale()
setYscale()
setPenRadius()
pygame.font.init()
# -----------------------------------------------------------------------
# Functions for displaying Tkinter dialog boxes in child processes.
def _getFileName():
"""Display a dialog box that asks the user for a file name."""
root = Tkinter.Tk()
root.withdraw()
reply = tkFileDialog.asksaveasfilename(initialdir=".")
sys.stdout.write(reply)
sys.stdout.flush()
sys.exit()
def _confirmFileSave():
"""Display a dialog box that confirms a file save operation."""
root = Tkinter.Tk()
root.withdraw()
tkMessageBox.showinfo(
title="File Save Confirmation", message="The drawing was saved to the file."
)
sys.exit()
def _reportFileSaveError(msg):
"""Display a dialog box that reports a msg.
msg is a string which describes an error in a file save operation.
"""
root = Tkinter.Tk()
root.withdraw()
tkMessageBox.showerror(title="File Save Error", message=msg)
sys.exit()
# -----------------------------------------------------------------------
def _regressionTest():
"""Perform regression testing."""
clear()
setPenRadius(0.5)
setPenColor(ORANGE)
point(0.5, 0.5)
show(0.0)
setPenRadius(0.25)
setPenColor(BLUE)
point(0.5, 0.5)
show(0.0)
setPenRadius(0.02)
setPenColor(RED)
point(0.25, 0.25)
show(0.0)
setPenRadius(0.01)
setPenColor(GREEN)
point(0.25, 0.25)
show(0.0)
setPenRadius(0)
setPenColor(BLACK)
point(0.25, 0.25)
show(0.0)
setPenRadius(0.1)
setPenColor(RED)
point(0.75, 0.75)
show(0.0)
setPenRadius(0)
setPenColor(CYAN)
for i in range(0, 100):
point(i / 512.0, 0.5)
point(0.5, i / 512.0)
show(0.0)
setPenRadius(0)
setPenColor(MAGENTA)
line(0.1, 0.1, 0.3, 0.3)
line(0.1, 0.2, 0.3, 0.2)
line(0.2, 0.1, 0.2, 0.3)
show(0.0)
setPenRadius(0.05)
setPenColor(MAGENTA)
line(0.7, 0.5, 0.8, 0.9)
show(0.0)
setPenRadius(0.01)
setPenColor(YELLOW)
circle(0.75, 0.25, 0.2)
show(0.0)
setPenRadius(0.01)
setPenColor(YELLOW)
filledCircle(0.75, 0.25, 0.1)
show(0.0)
setPenRadius(0.01)
setPenColor(PINK)
rectangle(0.25, 0.75, 0.1, 0.2)
show(0.0)
setPenRadius(0.01)
setPenColor(PINK)
filledRectangle(0.25, 0.75, 0.05, 0.1)
show(0.0)
setPenRadius(0.01)
setPenColor(DARK_RED)
square(0.5, 0.5, 0.1)
show(0.0)
setPenRadius(0.01)
setPenColor(DARK_RED)
filledSquare(0.5, 0.5, 0.05)
show(0.0)
setPenRadius(0.01)
setPenColor(DARK_BLUE)
polygon([0.4, 0.5, 0.6], [0.7, 0.8, 0.7])
show(0.0)
setPenRadius(0.01)
setPenColor(DARK_GREEN)
setFontSize(24)
text(0.2, 0.4, "hello, world")
show(0.0)
# import picture as p
# pic = p.Picture('saveIcon.png')
# picture(pic, .5, .85)
# show(0.0)
# Test handling of mouse and keyboard events.
setPenColor(BLACK)
from itu.algs4.stdlib import stdio
stdio.writeln("Left click with the mouse or type a key")
while True:
if mousePressed():
filledCircle(mouseX(), mouseY(), 0.02)
if hasNextKeyTyped():
stdio.write(nextKeyTyped())
show(0.0)
# Never get here.
show()
# -----------------------------------------------------------------------
def _main():
"""Dispatch to a function that does regression testing, or to a dialog-box-
handling function."""
import sys
if len(sys.argv) == 1:
_regressionTest()
elif sys.argv[1] == "getFileName":
_getFileName()
elif sys.argv[1] == "confirmFileSave":
_confirmFileSave()
elif sys.argv[1] == "reportFileSaveError":
_reportFileSaveError(sys.argv[2])
if __name__ == "__main__":
_main()
================================================
FILE: itu/algs4/stdlib/stdio.py
================================================
# code based on https://introcs.cs.princeton.edu/python/code/stdlib-python.zip as downloaded in dec 2017
"""stdio.py.
The stdio module supports reading from standard input and writing to
sys.stdout.
Note: Usually it's a bad idea to mix these three sets of reading
functions:
-- isEmpty(), readInt(), readFloat(), readBool(), readString()
-- hasNextLine(), readLine()
-- readAll(), readAllInts(), readAllFloats(), readAllBools(),
readAllStrings(), readAllLines()
Usually it's better to use one set exclusively.
"""
import re
import sys
# -----------------------------------------------------------------------
# Change sys.stdin so it provides universal newline support.
if sys.hexversion < 0x03000000:
import os
sys.stdin = os.fdopen(sys.stdin.fileno(), "rU", 0)
else:
sys.stdin = open(sys.stdin.fileno(), "r", newline=None)
# =======================================================================
# print to stderr
# from https://stackoverflow.com/questions/5574702/how-to-print-to-stderr-in-python#14981125
# =======================================================================
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
# =======================================================================
# Writing functions
# =======================================================================
def writeln(x=""):
"""Write x and an end-of-line mark to standard output."""
if sys.hexversion < 0x03000000:
print("Error: Python 3 is required.", file=sys.stderr)
sys.exit(1)
# x = unicode(x)
# x = x.encode("utf-8")
else:
x = str(x)
sys.stdout.write(x)
sys.stdout.write("\n")
sys.stdout.flush()
# -----------------------------------------------------------------------
def write(x=""):
"""Write x to standard output."""
if sys.hexversion < 0x03000000:
print("Error: Python 3 is required.", file=sys.stderr)
sys.exit(1)
# x = unicode(x)
# x = x.encode("utf-8")
else:
x = str(x)
sys.stdout.write(x)
sys.stdout.flush()
# -----------------------------------------------------------------------
def writef(fmt, *args):
"""Write each element of args to standard output.
Use the format specified by string fmt.
"""
x = fmt % args
if sys.hexversion < 0x03000000:
print("Error: Python 3 is required.", file=sys.stderr)
sys.exit(1)
# x = unicode(x)
# x = x.encode("utf-8")
sys.stdout.write(x)
sys.stdout.flush()
# =======================================================================
# Reading functions
# =======================================================================
_buffer = ""
# -----------------------------------------------------------------------
def _readRegExp(regExp):
"""Discard leading white space characters from standard input.
Then read from standard input and return a string matching regular
expression regExp. Raise an EOFError if no non-whitespace
characters remain in standard input. Raise a ValueError if the next
characters to be read from standard input do not match 'regExp'.
"""
global _buffer
if isEmpty():
raise EOFError()
compiledRegExp = re.compile(r"^\s*" + regExp)
match = compiledRegExp.search(_buffer)
if match is None:
raise ValueError()
s = match.group()
_buffer = _buffer[match.end() :]
return s.lstrip()
# -----------------------------------------------------------------------
def isEmpty():
"""Return True if no non-whitespace characters remain in standard input.
Otherwise return False.
"""
global _buffer
while _buffer.strip() == "":
line = sys.stdin.readline()
if sys.hexversion < 0x03000000:
line = line.decode("utf-8")
if line == "":
return True
_buffer += line
return False
# -----------------------------------------------------------------------
def readInt():
"""Discard leading white space characters from standard input.
Then read from standard input a sequence of characters comprising an
integer. Convert the sequence of characters to an integer, and
return the integer. Raise an EOFError if no non-whitespace
characters remain in standard input. Raise a ValueError if the next
characters to be read from standard input cannot comprise an
integer.
"""
s = _readRegExp(r"[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)")
radix = 10
strLength = len(s)
if (strLength >= 1) and (s[0:1] == "0"):
radix = 8
if (strLength >= 2) and (s[0:2] == "-0"):
radix = 8
if (strLength >= 2) and (s[0:2] == "0x"):
radix = 16
if (strLength >= 2) and (s[0:2] == "0X"):
radix = 16
if (strLength >= 3) and (s[0:3] == "-0x"):
radix = 16
if (strLength >= 3) and (s[0:3] == "-0X"):
radix = 16
return int(s, radix)
# -----------------------------------------------------------------------
def readAllInts():
"""Read all remaining strings from standard input, convert each to an int,
and return those ints in an array.
Raise a ValueError if any of the strings cannot be converted to an
int.
"""
strings = readAllStrings()
ints = []
for s in strings:
i = int(s)
ints.append(i)
return ints
# -----------------------------------------------------------------------
def readFloat():
"""Discard leading white space characters from standard input.
Then read from standard input a sequence of characters comprising a
float. Convert the sequence of characters to a float, and return the
float. Raise an EOFError if no non-whitespace characters remain in
standard input. Raise a ValueError if the next characters to be read
from standard input cannot comprise a float.
"""
s = _readRegExp(r"[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?")
return float(s)
# -----------------------------------------------------------------------
def readAllFloats():
"""Read all remaining strings from standard input, convert each to a float,
and return those floats in an array.
Raise a ValueError if any of the strings cannot be converted to a
float.
"""
strings = readAllStrings()
floats = []
for s in strings:
f = float(s)
floats.append(f)
return floats
# -----------------------------------------------------------------------
def readBool():
"""Discard leading white space characters from standard input. Then read
from standard input a sequence of characters comprising a bool. Convert the
sequence of characters to a bool, and return the bool. Raise an EOFError
if no non-whitespace characters remain in standard input. Raise a
ValueError if the next characters to be read from standard input cannot
comprise a bool.
These character sequences can comprise a bool:
-- True
-- False
-- 1 (means true)
-- 0 (means false)
"""
s = _readRegExp(r"(True)|(False)|1|0")
if (s == "True") or (s == "1"):
return True
return False
# -----------------------------------------------------------------------
def readAllBools():
"""Read all remaining strings from standard input, convert each to a bool,
and return those bools in an array.
Raise a ValueError if any of the strings cannot be converted to a
bool.
"""
strings = readAllStrings()
bools = []
for s in strings:
b = bool(s)
bools.append(b)
return bools
# -----------------------------------------------------------------------
def readString():
"""Discard leading white space characters from standard input.
Then read from standard input a sequence of characters comprising a
string, and return the string. Raise an EOFError if no non-
whitespace characters remain in standard input.
"""
s = _readRegExp(r"\S+")
return s
# -----------------------------------------------------------------------
def readAllStrings():
"""Read all remaining strings from standard input, and return them in an
array."""
strings = []
while not isEmpty():
s = readString()
strings.append(s)
return strings
# -----------------------------------------------------------------------
def hasNextLine():
"""Return True if standard input has a next line.
Otherwise return False.
"""
global _buffer
if _buffer != "":
return True
else:
_buffer = sys.stdin.readline()
if sys.hexversion < 0x03000000:
_buffer = _buffer.decode("utf-8")
if _buffer == "":
return False
return True
# -----------------------------------------------------------------------
def readLine():
"""Read and return as a string the next line of standard input.
Raise an EOFError is there is no next line.
"""
global _buffer
if not hasNextLine():
raise EOFError()
s = _buffer
_buffer = ""
return s.rstrip("\n")
# -----------------------------------------------------------------------
def readAllLines():
"""Read all remaining lines from standard input, and return them as strings
in an array."""
lines = []
while hasNextLine():
line = readLine()
lines.append(line)
return lines
# -----------------------------------------------------------------------
def readAll():
"""Read and return as a string all remaining lines of standard input."""
global _buffer
s = _buffer
_buffer = ""
for line in sys.stdin:
if sys.hexversion < 0x03000000:
line = line.decode("utf-8")
s += line
return s
# =======================================================================
# For Testing
# =======================================================================
def _testWrite():
writeln()
writeln("string")
writeln(123456)
writeln(123.456)
writeln(True)
write()
write("string")
write(123456)
write(123.456)
write(True)
writeln()
writef("<%s> <%8d> <%14.8f>\n", "string", 123456, 123.456)
writef("formatstring\n")
# -----------------------------------------------------------------------
def _main():
"""For testing.
The command-line argument should be the name of the function that
should be called.
"""
map = {
"readInt": readInt,
"readAllInts": readAllInts,
"readFloat": readFloat,
"readAllFloats": readAllFloats,
"readBool": readBool,
"readAllBools": readAllBools,
"readString": readString,
"readAllStrings": readAllStrings,
"readLine": readLine,
"readAllLines": readAllLines,
"readAll": readAll,
}
testId = sys.argv[1]
if testId == "write":
_testWrite()
else:
writeln(map[testId]())
if __name__ == "__main__":
_main()
================================================
FILE: itu/algs4/stdlib/stdrandom.py
================================================
# code based on https://introcs.cs.princeton.edu/python/code/stdlib-python.zip as downloaded in dec 2017
"""stdrandom.py.
The stdrandom module defines functions related to pseudo-random numbers.
"""
# -----------------------------------------------------------------------
import math
import random
# -----------------------------------------------------------------------
def seed(i=None):
"""Seed the random number generator as hash(i), where i is an int.
If i is None, then seed using the current time or, quoting the help
page for random.seed(), "an operating system specific randomness
source if available."
"""
random.seed(i)
# -----------------------------------------------------------------------
def uniform(hi):
"""Return an integer chosen uniformly from the range [0, hi)."""
return random.randrange(0, hi)
# -----------------------------------------------------------------------
def uniformInt(lo, hi):
"""Return an integer chosen uniformly from the range [lo, hi)."""
return random.randrange(lo, hi)
# -----------------------------------------------------------------------
def uniformFloat(lo, hi):
"""Return a number chosen uniformly from the range [lo, hi)."""
return random.uniform(lo, hi)
# -----------------------------------------------------------------------
def bernoulli(p=0.5):
"""Return True with probability p."""
return random.random() < p
# -----------------------------------------------------------------------
def binomial(n, p=0.5):
"""Return the number of heads in n coin flips, each of which is heads with
probability p."""
heads = 0
for i in range(n):
if bernoulli(p):
heads += 1
return heads
# -----------------------------------------------------------------------
def gaussian(mean=0.0, stddev=1.0):
"""Return a float according to a standard Gaussian distribution with the
given mean (mean) and standard deviation (stddev)."""
# Approach 1:
# return random.gauss(mu, sigma)
# Approach 2: Use the polar form of the Box-Muller transform.
x = uniformFloat(-1.0, 1.0)
y = uniformFloat(-1.0, 1.0)
r = x * x + y * y
while (r >= 1) or (r == 0):
x = uniformFloat(-1.0, 1.0)
y = uniformFloat(-1.0, 1.0)
r = x * x + y * y
g = x * math.sqrt(-2 * math.log(r) / r)
# Remark: x * math.sqrt(-2 * math.log(r) / r)
# is an independent random gaussian
return mean + stddev * g
# -----------------------------------------------------------------------
def discrete(a):
"""Return a float from a discrete distribution: i with probability a[i].
Precondition: the elements of array a sum to 1.
"""
r = uniformFloat(0.0, sum(a))
subtotal = 0.0
for i in range(len(a)):
subtotal += a[i]
if subtotal > r:
return i
# return len(a) - 1
# -----------------------------------------------------------------------
def shuffle(a):
"""Shuffle array a."""
# Approach 1:
# for i in range(len(a)):
# j = i + uniformInt(len(a) - i)
# temp = a[i]
# a[i] = a[j]
# a[j] = temp
# Approach 2:
random.shuffle(a)
# -----------------------------------------------------------------------
def exp(lambd):
"""Return a float from an exponential distribution with rate lambd."""
# Approach 1:
# return random.expovariate(lambd)
# Approach 2:
return -math.log(1 - random.random()) / lambd
# -----------------------------------------------------------------------
def _main():
"""For testing."""
import sys
from itu.algs4.stdlib import stdio
seed(1)
n = int(sys.argv[1])
for i in range(n):
stdio.writef(" %2d ", uniformInt(10, 100))
stdio.writef("%8.5f ", uniformFloat(10.0, 99.0))
stdio.writef("%5s ", bernoulli())
stdio.writef("%5s ", binomial(100, 0.5))
stdio.writef("%7.5f ", gaussian(9.0, 0.2))
stdio.writef("%2d ", discrete([0.5, 0.3, 0.1, 0.1]))
stdio.writeln()
if __name__ == "__main__":
_main()
# -----------------------------------------------------------------------
# python stdrandom.py 5
# 27 60.65914 False 41 9.01682 0
# 55 46.88378 True 48 8.90171 0
# 58 92.96468 True 52 9.12770 0
# 79 64.41387 False 47 9.49241 0
# 29 32.30299 True 45 8.77630 1
================================================
FILE: itu/algs4/stdlib/stdstats.py
================================================
# code based on https://introcs.cs.princeton.edu/python/code/stdlib-python.zip as downloaded in dec 2017
"""stdstats.py.
The stdstats module defines functions related to statistical analysis
and graphical data display.
"""
# -----------------------------------------------------------------------
import math
from itu.algs4.stdlib import stddraw
# -----------------------------------------------------------------------
# def min(a):
# """
# Return the minimum value in array a. Could call the built-in
# min() function instead.
# """
# minumum = float('inf')
# for x in a:
# if x < minumum:
# minumum = x
# return minumum
# -----------------------------------------------------------------------
# def max(a):
# """
# Return the maximum value in array a. Could call the built-in
# max() function instead.
# """
# maximum = float('-inf')
# for x in a:
# if x > maximum:
# maximum = x
# return maximum
# -----------------------------------------------------------------------
def mean(a):
"""Return the average of the elements of array a."""
return sum(a) / float(len(a))
# -----------------------------------------------------------------------
def var(a):
"""Return the sample variance of the elements of array a."""
mu = mean(a)
total = 0.0
for x in a:
total += (x - mu) * (x - mu)
return total / (float(len(a)) - 1.0)
# -----------------------------------------------------------------------
def stddev(a):
"""Return the standard deviation of the elements of array a."""
return math.sqrt(var(a))
# -----------------------------------------------------------------------
def median(a):
"""Return the median of the elements of array a."""
b = list(a) # Make a copy of a.
b.sort()
length = len(b)
if length % 2 == 1:
return b[length // 2]
else:
return float(b[length // 2 - 1] + b[length // 2]) / 2.0
# -----------------------------------------------------------------------
def plotPoints(a):
"""Plot the elements of array a as points."""
n = len(a)
stddraw.setXscale(-1, n)
stddraw.setPenRadius(1.0 / (3.0 * n))
for i in range(n):
stddraw.point(i, a[i])
# -----------------------------------------------------------------------
def plotLines(a):
"""Plot the elements of array a as line end-points."""
n = len(a)
stddraw.setXscale(-1, n)
stddraw.setPenRadius(0.0)
for i in range(1, n):
stddraw.line(i - 1, a[i - 1], i, a[i])
# -----------------------------------------------------------------------
def plotBars(a):
"""Plot the elements of array a as bars."""
n = len(a)
stddraw.setXscale(-1, n)
for i in range(n):
stddraw.filledRectangle(i - 0.25, 0.0, 0.5, a[i])
# -----------------------------------------------------------------------
def _main():
"""For testing:"""
import stdarray
import stdio
a = stdarray.readFloat1D()
# stdio.writef(' min %7.3f\n', min(a))
# stdio.writef(' max %7.3f\n', max(a))
stdio.writef(" mean %7.3f\n", mean(a))
stdio.writef(" std dev %7.3f\n", stddev(a))
stdio.writef(" median %7.3f\n", median(a))
if __name__ == "__main__":
_main()
================================================
FILE: itu/algs4/strings/__init__.py
================================================
================================================
FILE: itu/algs4/strings/boyer_moore.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
import sys
class BoyerMoore:
"""The BoyerMoore class finds the first occurence of a pattern string in a
text string.
This implementation uses the Boyer-Moore algorithm (with the bad-
character rule, but not the strong good suffix rule).
"""
def __init__(self, pat):
"""Preprocesses the pattern string.
:param pat: the pattern string
"""
self.pat = pat
M = len(pat)
R = 256
self.right = [-1 for i in range(0, R)] # -1 for chars not in pattern
for j in range(0, M):
self.right[ord(pat[j])] = j
def search(self, txt):
"""Returns the index of the first occurrrence of the pattern string in
the text string.
:param txt: the text string
:return: the index of the first occurrence of the pattern string
in the text string; N if no such match
"""
N = len(txt)
M = len(self.pat)
skip = 0
i = 0
# for i in range(0,N-M+1,skip):
while i <= N - M:
skip = 0
for j in range(M - 1, -1, -1):
if not (self.pat[j] == txt[i + j]):
skip = j - self.right[ord(txt[i + j])]
if skip < 1:
skip = 1
break
if skip == 0:
return i
i += skip
return N
def main():
"""Takes a pattern string and an input string as command-line arguments;
searches for the pattern string in the text string; and prints the first
occurrence of the pattern string in the text string.
Will print the pattern after the end of the string if no match is
found.
"""
pat = sys.argv[1]
txt = sys.argv[2]
bm = BoyerMoore(pat)
print("text: {}".format(txt))
offset = bm.search(txt)
print("pattern:", end=" ")
for _ in range(0, offset):
print("", end=" ")
print(pat)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/strings/huffman_compression.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
"""The Huffman compression module provides static methods for compressing and
expanding a binary input using Huffman codes over the 8-bit extended ASCII
alphabet.
For additional documentation, see Section 5.5 of Algorithms, 4th
edtition by Robert Sedgewick and Kevin Wayne.
"""
import sys
from itu.algs4.sorting.min_pq import MinPQ
from itu.algs4.stdlib.binary_stdin import BinaryStdIn
from itu.algs4.stdlib.binary_stdout import BinaryStdOut
_R = 256
class _Node:
def __init__(self, ch, freq, left, right):
self.ch = ch
self.freq = freq
self.left = left
self.right = right
def is_leaf(self):
left = self.left
right = self.right
assert left is None and right is None or left is not None and right is not None
return left is None and right is None
def __gt__(self, that):
return self.freq > that.freq
def compress():
"""Reads a sequence of 8-bit bytes from standard input; compresses them using
Huffman codes with an 8-bit alphabet; and writes the results to standard
input."""
s = BinaryStdIn.read_string()
# Tabulate frequency counts
freq = [0 for i in range(0, _R)]
for i in range(0, len(s)):
freq[ord(s[i])] += 1
# Build Huffman trie
root = _build_trie(freq)
# Build code table
st = [None for i in range(0, _R)]
_build_code(st, root, "")
# Print trie for decoder
_write_trie(root)
# Print number of bytes in original uncompressed message
BinaryStdOut.write_int(len(s))
# Use Huffman code to encode input
for i in range(0, len(s)):
code = st[ord(s[i])]
for j in range(0, len(code)):
if code[j] == "0":
BinaryStdOut.write_bool(False)
elif code[j] == "1":
BinaryStdOut.write_bool(True)
else:
raise ValueError("Illegal state")
BinaryStdOut.close()
# Build the Huffman trie given frequencies
def _build_trie(freq):
pq = MinPQ()
for i in range(0, _R):
if freq[i] > 0:
pq.insert(_Node(chr(i), freq[i], None, None))
if pq.size() == 0:
raise ValueError("The provided file is empty")
if pq.size() == 1:
if freq[ord("\0")] == 0:
pq.insert(_Node("\0", 0, None, None))
else:
pq.insert(_Node("\1", 0, None, None))
while pq.size() > 1:
left = pq.del_min()
right = pq.del_min()
parent = _Node("\0", left.freq + right.freq, left, right)
pq.insert(parent)
return pq.del_min()
# Write bitstring-encoded trie to standard output
def _write_trie(x):
if x.is_leaf():
BinaryStdOut.write_bool(True)
BinaryStdOut.write_char(x.ch)
return
BinaryStdOut.write_bool(False)
_write_trie(x.left)
_write_trie(x.right)
# Make a lookup table from symbols and their encodings
def _build_code(st, x, s):
if not x.is_leaf():
_build_code(st, x.left, s + "0")
_build_code(st, x.right, s + "1")
else:
st[ord(x.ch)] = s
def expand():
"""Reads a sequence of bits that represents a Huffman-compressed message from
standard input; expands them; and writes the results to standard output."""
BinaryStdIn.is_empty()
root = _read_trie()
length = BinaryStdIn.read_int()
for _ in range(0, length):
x = root
while not x.is_leaf():
bit = BinaryStdIn.read_bool()
if bit:
x = x.right
else:
x = x.left
BinaryStdOut.write_char(x.ch)
BinaryStdOut.close()
def _read_trie():
isLeaf = BinaryStdIn.read_bool()
if isLeaf:
return _Node(BinaryStdIn.read_char(), -1, None, None)
else:
return _Node("\0", -1, _read_trie(), _read_trie())
def main():
"""Sample client that calss compress() if the command-line argument is "-",
and expand() if it is "+"."""
if sys.argv[1] == "-":
compress()
elif sys.argv[1] == "+":
expand()
else:
raise ValueError("Illegal command line argument")
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/strings/kmp.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
"""
The KMP (Knuth-Morris-Pratt) class finds the first occurrence
of a pattern string in a text string.
This implementation uses a version of the Knuth-Morris-Pratt substring search
algorithm. The version takes time as space proportional to
N + M R in the worst case, where N is the length
of the text string, M is the length of the pattern, and R
is the alphabet size.
"""
class KMP:
def __init__(self, pat):
"""Preprocesses the pattern string.
:param pat: the pattern string
"""
self.pat = pat
M = len(pat)
R = 256
self.dfa = [[0 for c in range(0, M)] for r in range(0, R)]
self.dfa[ord(pat[0])][0] = 1
X = 0
for j in range(1, M):
# Compute dfa[][j]
for c in range(0, R):
self.dfa[c][j] = self.dfa[c][X] # Copy mismatch cases
self.dfa[ord(pat[j])][j] = j + 1 # Set match case
X = self.dfa[ord(pat[j])][X] # Update restart state
def search(self, txt):
"""Returns the index of the first occurrrence of the pattern string in the
text string.
:param txt: the text string
:return: the index of the first occurrence of the pattern string
in the text string; N if no such match
"""
N = len(txt)
M = len(self.pat)
j = 0
i = 0
while i < N and j < M:
j = self.dfa[ord(txt[i])][j]
i += 1
if j == M: # found (hit end of pattern)
return i - M
else: # not found (hit end of text)
return N
def main():
"""Takes a pattern string and an input string as command-line arguments;
searches for the pattern string in the text string; and prints the first
occurrence of the pattern string in the text string.
Will print the pattern after the end of the string if no match is
found.
"""
pat = sys.argv[1]
txt = sys.argv[2]
kmp = KMP(pat)
print("text: {}".format(txt))
offset = kmp.search(txt)
print("pattern: ", end="")
for _ in range(0, offset):
print("", end=" ")
print(pat)
if __name__ == "__main__":
import sys
main()
================================================
FILE: itu/algs4/strings/lsd.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
"""This module provides functions for sorting arrays of strings using lsd sort.
For additional documentation, see Section 5.1 of Algorithms, 4th Edition
by Robert Sedgewick and Kevin Wayne.
"""
def sort(a, w, radix=256):
"""Rearranges the array of w-character strings in ascending order.
:param a: the array to be sorted
:param w: the number of characters per string
:param radix: an optional number specifying the size of the alphabet to sort
"""
n = len(a)
aux = [None] * n
for d in range(w - 1, -1, -1): # from w-i to 0
# sort by key-indexed counting on dth character
# compute frequency counts
count = [0] * (radix + 1)
for i in range(n):
ch = ord(a[i][d]) # get number representation of character
count[ch + 1] += 1
# compute cumulates
for r in range(radix):
count[r + 1] += count[r]
# move data
for i in range(n):
ch = ord(a[i][d])
aux[count[ch]] = a[i]
count[ch] += 1
# copy back
for i in range(n):
a[i] = aux[i]
if __name__ == "__main__":
import sys
from itu.algs4.stdlib import stdio
if len(sys.argv) > 1:
try:
sys.stdin = open(sys.argv[1])
except IOError:
print("File not found, using standard input instead")
a = stdio.readAllStrings()
sort(a, 3)
for elem in a:
print(elem)
================================================
FILE: itu/algs4/strings/lzw.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
"""The LSW module provides static methods for compressing and expanding a
binary input using LZW over the 8-bit extended ASCII alphabet with 12-bit
codewords.
For additional documentation see Section 5.5 of Algorithms, 4th Edition
by Robert Sedgewick and Kevin Wayne.
"""
import sys
from itu.algs4.stdlib.binary_stdin import BinaryStdIn
from itu.algs4.stdlib.binary_stdout import BinaryStdOut
from itu.algs4.strings.tst import TST
_R = 256
_L = 4096
_W = 12
def compress():
"""Reads a sequence of 8-bit bytes from standard input; compresses them using
LZW compression with 12-bit codewords; and writes the results to standard
output."""
input_ = BinaryStdIn.read_string()
st = TST()
for i in range(0, _R):
st.put("" + chr(i), i)
code = _R + 1
while len(input_) > 0:
s = st.longest_prefix_of(input_)
BinaryStdOut.write_int(st.get(s), _W)
t = len(s)
if t < len(input_) and code < _L:
st.put(input_[0 : t + 1], code)
code += 1
input_ = input_[t:]
BinaryStdOut.write_int(_R, _W)
BinaryStdOut.close()
def expand():
"""Reads a sequence of bit encoded using LZW compression with 12-bit codewords
from standard input; expands them; and writes the results to standard
output."""
st = ["" for i in range(0, _L)]
i = 0
while i < _R:
st[i] = "" + chr(i)
i += 1
st[i] = ""
i += 1
codeword = BinaryStdIn.read_int(_W)
if codeword == _R:
return
val = st[codeword]
while True:
BinaryStdOut.write_string(val)
codeword = BinaryStdIn.read_int(_W)
if codeword == _R:
break
s = st[codeword]
if i == codeword:
s = val + val[0]
if i < _L:
st[i] = val + s[0]
i += 1
val = s
BinaryStdOut.close()
def main():
"""Sample client that calls compress() if the command-line argument is "-",
and expand() if it is "+".
Example: echo huhu | python3 algs4/strings/lzw.py - | python3 algs4/strings/lzw.py +
"""
if sys.argv[1] == "-":
compress()
elif sys.argv[1] == "+":
expand()
else:
raise ValueError("Illegal command line argument")
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/strings/msd.py
================================================
# Created for BADS 2018
# See README.md for details
# Python 3
"""This module provides functions for sorting arrays of strings using msd sort.
For additional documentation, see Section 5.1 of Algorithms, 4th Edition
by Robert Sedgewick and Kevin Wayne.
"""
cutoff = 15
# compare characters at position d
def _less(a, b, d):
return a[d] < b[d]
# insertion sort a[lo..hi], starting at dth character
def _insertion(a, lo, hi, d):
for i in range(lo, hi + 1):
j = i
while j > lo and _less(a[j], a[j - 1], d):
a[j], a[j - 1] = a[j - 1], a[j]
j -= 1
# sort from a[lo] to a[hi], starting at the dth character
def _sort(a, lo, hi, d, aux, radix):
if hi <= lo + cutoff:
_insertion(a, lo, hi, d)
return
# compute frequency counts
count = [0] * (radix + 2)
for i in range(hi + 1):
ch = ord(a[i][d]) # get number representation of character
count[ch + 2] += 1
# compute cumulates
for r in range(radix + 1):
count[r + 1] += count[r]
# move data
for i in range(hi + 1):
ch = ord(a[i][d])
aux[count[ch + 1]] = a[i]
count[ch + 1] += 1
# copy back
for i in range(hi + 1):
a[i] = aux[i - lo]
# recursively sort for each character (excludes sentinel -1)
for r in range(radix):
_sort(a, lo + count[r], lo + count[r + 1] - 1, d + 1, aux, radix)
def sort(a, radix=256):
"""Rearranges the array of 32-bit integers in ascending order. Currently
assumes that the integers are nonnegative.
:param a: the array to be sorted
"""
n = len(a)
aux = [None] * n
_sort(a, 0, n - 1, 0, aux, radix)
if __name__ == "__main__":
import sys
from itu.algs4.stdlib import stdio
if len(sys.argv) > 1:
try:
sys.stdin = open(sys.argv[1])
except IOError:
print("File not found, using standard input instead")
a = stdio.readAllStrings()
sort(a)
for elem in a:
print(elem)
================================================
FILE: itu/algs4/strings/nfa.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
import sys
from itu.algs4.fundamentals.bag import Bag
from itu.algs4.fundamentals.stack import Stack
from itu.algs4.graphs.digraph import Digraph
from itu.algs4.graphs.directed_dfs import DirectedDFS
class NFA:
"""The NFA class provides a data type for creating a nondeterministic finite
state automaton (NFA) from a regular expression and testing whether a given
string is matched by that regular expression. It supports the following
operations: concatenation, closure, binary or, and parentheses, metacharacters
(either in the text or pattern), capturing capabilities, greedy or
reluctant/lazy modifiers, and other features in industrial-strength
implementations such as Java's Pattern and Matcher.
This implementation builds the NFA using a digraph and a stack
and simulates the NFA using digraph search (see the textbook for details).
The constructor takes time proportional to m, where m is the
number of characters in the regular expression.
The recognizes() method takes time proportional to m*n, where n
is the number of characters in the text.
For additional documentation, see section 5.4 of Algorithms, 4th Edition
by Robert Sedgewick and Kevin Wayne.
"""
def __init__(self, regex):
"""Initializes the NFA from the specified regular expression.
:param regex: the regular expression
"""
self.regex = regex
m = len(regex)
self.m = m
ops = Stack()
graph = Digraph(m + 1)
for i in range(0, m):
lp = i
if regex[i] == "(" or regex[i] == "|":
ops.push(i)
elif regex[i] == ")":
or_ = ops.pop()
# 2-way or operator
if regex[or_] == "|":
lp = ops.pop()
graph.add_edge(lp, or_ + 1)
graph.add_edge(or_, i)
elif regex[or_] == "(":
lp = or_
else:
assert False
if i < m - 1 and regex[i + 1] == "*":
graph.add_edge(lp, i + 1)
graph.add_edge(i + 1, lp)
if regex[i] == "(" or regex[i] == "*" or regex[i] == ")":
graph.add_edge(i, i + 1)
if ops.size() != 0:
raise ValueError("Invalid regular expression")
self.graph = graph
def recognizes(self, txt):
"""Returns True if the text is matched by the regular expression.
:param txt: the text
:returns: True if the text is matched by the regular expression;
False otherwise
"""
regex = self.regex
graph = self.graph
m = self.m
dfs = DirectedDFS(graph, 0)
pc = Bag()
for v in range(0, graph.V()):
if dfs.is_marked(v):
pc.add(v)
# Compute possible NFA states for txt[i+1]
for i in range(0, len(txt)):
if txt[i] == "*" or txt[i] == "|" or txt[i] == "(" or txt[i] == ")":
raise ValueError("text contains the metacharacter '{}'".format(txt[i]))
match = Bag()
for v in pc:
if v == m:
continue
if regex[v] == txt[i] or regex[i] == ".":
match.add(v + 1)
dfs = DirectedDFS(graph, *match)
pc = Bag()
for v in range(0, graph.V()):
if dfs.is_marked(v):
pc.add(v)
# Optimization if no states reachable
if pc.size() == 0:
return False
for v in pc:
if v == m:
return True
return False
def main():
"""Unit tests the NFA data type."""
regex = "({})".format(sys.argv[1])
txt = sys.argv[2]
nfa = NFA(regex)
print(nfa.recognizes(txt))
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/strings/quick3string.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
"""The Quick3String module provides functions for sorting an array of strings
using 3-way radix quicksort."""
import sys
def sort(a):
"""Rearranges the array of strings in ascending order.
:param a: the array to be sorted.
"""
_sort(a, 0, len(a) - 1, 0)
def _sort(a, lo, hi, d):
if hi <= lo:
return
lt = lo
gt = hi
v = _char_at(a[lo], d)
i = lo + 1
while i <= gt:
t = _char_at(a[i], d)
if t < v:
_exch(a, lt, i)
lt += 1
i += 1
elif t > v:
_exch(a, i, gt)
gt -= 1
else:
i += 1
_sort(a, lo, lt - 1, d)
if v >= 0:
_sort(a, lt, gt, d + 1)
_sort(a, gt + 1, hi, d)
def _char_at(s, d):
if d < len(s):
return ord(s[d])
else:
return -1
def _show(a):
for item in a:
print(item)
def is_sorted(a):
"""Returns true if a is sorted.
:param a: the array to be checked.
:returns: True if a is sorted.
"""
for i in range(1, len(a)):
if _less(a[i], a[i - 1]):
return False
return True
def _less(v, w):
return v < w
def _exch(a, i, j):
t = a[i]
a[i] = a[j]
a[j] = t
def main():
"""Reads in a sequence of fixed-length strings from standard input; 3-way
radix quicksorts them; and prints them to standard output in ascending
order."""
a = sys.argv[1:]
sort(a)
assert is_sorted(a)
_show(a)
if __name__ == "__main__":
main()
================================================
FILE: itu/algs4/strings/rabin_karp.py
================================================
# Created for BADS 2018
# See README.md for details
# This is python3
import math
import random
# The following part is borrowed from https://langui.sh/2009/03/07/generating-very-large-primes/
# in an effort to implement the missing long_random_prime() function
def _rabin_miller(n):
s = n - 1
t = 0
while s & 1 == 0:
s = int(s / 2)
t += 1
k = 0
while k < 128:
a = random.randrange(2, n - 1)
# a^s is computationally infeasible. we need a more intelligent approach
# v = (a**s)%n
# python's core math module can do modular exponentiation
v = pow(a, s, n) # where values are (num,exp,mod)
if v != 1:
i = 0
while v != (n - 1):
if i == t - 1:
return False
else:
i = i + 1
v = (v ** 2) % n
k += 2
return True
def _is_prime(n):
# lowPrimes is all primes (sans 2, which is covered by the bitwise and operator)
# under 1000. taking n modulo each lowPrime allows us to remove a huge chunk
# of composite numbers from our potential pool without resorting to Rabin-Miller
lowPrimes = [
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97,
101,
103,
107,
109,
113,
127,
131,
137,
139,
149,
151,
157,
163,
167,
173,
179,
181,
191,
193,
197,
199,
211,
223,
227,
229,
233,
239,
241,
251,
257,
263,
269,
271,
277,
281,
283,
293,
307,
311,
313,
317,
331,
337,
347,
349,
353,
359,
367,
373,
379,
383,
389,
397,
401,
409,
419,
421,
431,
433,
439,
443,
449,
457,
461,
463,
467,
479,
487,
491,
499,
503,
509,
521,
523,
541,
547,
557,
563,
569,
571,
577,
587,
593,
599,
601,
607,
613,
617,
619,
631,
641,
643,
647,
653,
659,
661,
673,
677,
683,
691,
701,
709,
719,
727,
733,
739,
743,
751,
757,
761,
769,
773,
787,
797,
809,
811,
821,
823,
827,
829,
839,
853,
857,
859,
863,
877,
881,
883,
887,
907,
911,
919,
929,
937,
941,
947,
953,
967,
971,
977,
983,
991,
997,
]
if n >= 3:
if n & 1 != 0:
for p in lowPrimes:
if n == p:
return True
if n % p == 0:
return False
return _rabin_miller(n)
return False
def long_random_prime(k):
"""Generates a random prime.
:param k: the desired bit length of the prime
:returns: a random prime of bit length k
"""
# k is the desired bit length
r = 100 * (math.log(k, 2) + 1) # number of attempts max
r_ = r
while r > 0:
# randrange is mersenne twister and is completely deterministic
# unusable for serious crypto purposes
n = random.randrange(2 ** (k - 1), 2 ** (k))
r -= 1
if _is_prime(n):
return n
return "Failure after " + r_ + " tries."
class RabinKarp:
"""The RabinKarp class finds the first occurence of a pattern string in a
text string.
This implementation uses the Monte Carlo version of the Rabin-Karp
algorithm.
"""
def __init__(self, pat):
"""Preprocesses the pattern string.
:param pat: the pattern string
"""
self.M = len(pat)
self.R = 256
self.Q = long_random_prime(32)
self.RM = 1
for _ in range(1, self.M):
self.RM = (self.R * self.RM) % self.Q
self.patHash = self._hash(pat, self.M) # pattern hash value
def search(self, txt):
"""Returns the index of the first occurrrence of the pattern string in
the text string.
:param txt: the text string
:return: the index of the first occurrence of the pattern string
in the text string; N if no such match
"""
N = len(txt)
M = self.M
RM = self.RM
Q = self.Q
R = self.R
patHash = self.patHash
txtHash = self._hash(txt, M)
if patHash == txtHash:
return 0
for i in range(M, N):
txtHash = (txtHash + Q - RM * ord(txt[i - M]) % Q) % Q
txtHash = (txtHash * R + ord(txt[i])) % Q
if patHash == txtHash:
if self._check(i - M + 1):
return i - M + 1
return N
def _check(self, i):
# Not needed in the Monte Carlo version
# For Las Vegas, check pat with txt[i:i-M+1]
return True
def _hash(self, key, M):
# Modular hashing using Horner's method
h = 0
for j in range(0, M):
h = self.R * h + ord(key[j]) % self.Q
return h
def main():
"""Takes a pattern string and an input string as command-line arguments;
searches for the pattern string in the text string; and prints the first
occurrence of the pattern string in the text string.
Will print the pattern after the end of the string if no match is
found.
"""
pat = sys.argv[1]
txt = sys.argv[2]
rk = RabinKarp(pat)
print("text: {}".format(txt))
offset = rk.search(txt)
print("pattern:", end=" ")
for _ in range(0, offset):
print("", end=" ")
print(pat)
if __name__ == "__main__":
import sys
main()
================================================
FILE: itu/algs4/strings/trie_st.py
================================================
# created for BADS 2018
# See README.md for details
# Python 3
import sys
from itu.algs4.fundamentals.queue import Queue
from itu.algs4.stdlib import stdio
try:
q = Queue()
q.enqueue(1)
except AttributeError:
print("ERROR - Could not import itu.algs4 queue")
sys.exit(1)
"""
* The TrieST class represents an symbol table of key-value
* pairs, with string keys and generic values.
* It supports the usual put, get, contains,
* delete, size, and is-empty methods.
* It also provides character-based methods for finding the string
* in the symbol table that is the longest prefix of a given prefix,
* finding all strings in the symbol table that start with a given prefix,
* and finding all strings in the symbol table that match a given pattern.
* A symbol table implements the associative array abstraction:
* when associating a value with a key that is already in the symbol table,
* the convention is to replace the old value with the new value.
* This class uses the convention that
* values cannot be None, setting the
* value associated with a key to None is equivalent to deleting the key
* from the symbol table.
* This implementation uses a 256-way trie.
* The put, contains, delete, and
* longest prefix operations take time proportional to the length
* of the key (in the worst case). Construction takes constant time.
* The size, and is-empty operations take constant time.
"""
class TrieST(object):
R = 256 # extended ASCII
# R-way trie node
class Node(object):
R = 256
def __init__(self):
self.val = None
self.next = [None] * self.R # array of nodes of length R
def __init__(self):
self._root = self.Node() # root of trie
self._n = 0 # number of keys in trie
# Returns the value associated with the given key.
# @param key: the key
# @return: the value associated with the given key if the key is in the symbol table
# and None if the key is not in the symbol table
# @raises TypeError if key is None
def get(self, key):
x = self._get(self._root, key, 0)
return None if x is None else x.val
# Does this symbol table contain the given key?
# @param key: the key
# @returns True if this symbol table contains key and
# False otherwise
# @raises TypeError if key is None
def contains(self, key):
return self.get(key) is not None
def _get(self, x, key, d):
if x is None:
return None
if d == len(key):
return x
c = key[d]
return self._get(x.next[ord(c)], key, d + 1)
# Inserts the key-value pair into the symbol table, overwriting the old value
# with the new value if the key is already in the symbol table.
# If the value is None, this effectively deletes the key from the symbol table.
# @param key: the key
# @param val: the value
# @raises TypeError if key is None
def put(self, key, val):
if val is None:
self.delete(key)
else:
self._root = self._put(self._root, key, val, 0)
def _put(self, x, key, val, d):
if x is None:
x = self.Node()
if d == len(key):
if x.val is None:
self._n += 1
x.val = val
return x
c = key[d]
x.next[ord(c)] = self._put(x.next[ord(c)], key, val, d + 1)
return x
# @return the number of key-value pairs in this symbol table
def size(self):
return self._n
def __len__(self):
return self.size()
# Is this symbol table empty?
# @return True if this symbol table is empty and False otherwise
def is_empty(self):
return self.size() == 0
# Returns all keys in the symbol table as an iterable object.
# To iterate over all of the keys in the symbol table named st,
# use the foreach notation: for key in st.keys().
# @return all keys in the symbol table as an iterable object
def keys(self):
return self.keys_with_prefix("")
# Returns all of the keys in the set that start with prefix.
# @param prefix: the prefix
# @return all of the keys in the set that start with prefix,
# as an iterable
def keys_with_prefix(self, prefix):
results = Queue()
x = self._get(self._root, prefix, 0)
self._collect(x, prefix, results)
return results
def _collect(self, x, prefix, results):
if x is None:
return
if x.val is not None:
results.enqueue(prefix)
for c in range(0, self.R):
self._collect(x.next[c], prefix + chr(c), results)
# Returns all of the keys in the symbol table that match pattern,
# where . symbol is treated as a wildcard character.
# @param pattern the pattern
# @return all of the keys in the symbol table that match pattern,
# as an iterable, where . is treated as a wildcard character.
def keys_that_match(self, pattern):
results = Queue()
self._collect_match(self._root, "", pattern, results)
return results
def _collect_match(self, x, prefix, pattern, results):
if x is None:
return None
d = len(prefix)
if d == len(pattern) and x.val is not None:
results.enqueue(prefix)
if d >= len(pattern):
return
c = pattern[d]
if c == ".":
for c in range(0, self.R):
self._collect_match(x.next[c], prefix + chr(c), pattern, results)
else:
self._collect_match(x.next[ord(c)], prefix + c, pattern, results)
# Returns the string in the symbol table that is the longest prefix of query,
# or None, if no such string.
# @param query the query string
# @return the string in the symbol table that is the longest prefix of query,
# or None if no such string
# @raises TypeError if query is None
def longest_prefix_of(self, query):
length = self._longest_prefix_of(self._root, query, 0, -1)
if length == -1:
return None
else:
return query[:length]
# returns the length of the longest string key in the subtrie
# rooted at x that is a prefix of the query string,
# assuming the first d character match and we have already
# found a prefix match of given length (-1 if no such match)
def _longest_prefix_of(self, x, query, d, length):
if x is None:
return length
if x.val is not None:
length = d
if d == len(query):
return length
c = query[d]
return self._longest_prefix_of(x.next[ord(c)], query, d + 1, length)
# Removes the key from the set if the key is present.
# @param key the key
# @raises TypeError if key is None
def delete(self, key):
self._root = self._delete(self._root, key, 0)
def _delete(self, x, key, d):
if x is None:
return None
if d == len(key):
if x.val is not None:
self._n += -1
x.val = None
else:
c = key[d]
x.next[ord(c)] = self._delete(x.next[ord(c)], key, d + 1)
# remove subtrie rooted at x if it is completely empty
if x.val is not None:
return x
for c in range(0, self.R):
if x.next[c] is not None:
return x
return None
def test():
st = TrieST()
st.put("abc", 0)
keys = [k for k in st.keys()]
assert keys == ["abc"]
assert st.get("abc") == 0
st.put("a", 1)
st.put("b", 2)
st.put("c", 3)
st.delete("abc")
assert "abc" not in [k for k in st.keys()]
assert st.get("a") == 1
assert st.get("b") == 2
assert st.get("c") == 3
st.put("hello", 10)
assert st.contains("hello")
assert st.contains("not there") is False
st.put("he", 20)
assert st.longest_prefix_of("hell") == "he"
st.put("jello", 30)
q = st.keys_that_match(".e.l.")
assert q.size() == 2 and "jello" in q and "hello" in q
print("tests passed.")
if __name__ == "__main__":
test()
st = TrieST()
i = 0
print("Insert keys (Ctrl-D to stop):")
while not stdio.isEmpty():
key = stdio.readString()
st.put(key, i)
i += 1
# print results
if st.size() < 100:
print('keys(""):')
for key in st.keys():
print("{} {}".format(key, st.get(key)))
print()
print('longest_prefix_of("shellsort"):')
print(st.longest_prefix_of("shellsort"))
print()
print('longest_prefix_of("quicksort")')
print(st.longest_prefix_of("quicksort"))
print()
print('keys_with_prefix("shor")')
for s in st.keys_with_prefix("shor"):
print(s)
print()
print('keys_that_match("he.l.")')
for s in st.keys_that_match("he.l."):
print()
================================================
FILE: itu/algs4/strings/tst.py
================================================
# created for BADS 2018
# see README.md for details
# Python 3
import sys
from itu.algs4.fundamentals.queue import Queue
from itu.algs4.stdlib import stdio
try:
q = Queue()
q.enqueue(1)
except AttributeError:
print("ERROR - unable to import python algs4 Queue class")
sys.exit(1)
# Symbol table with string keys, implemented using a ternary search
# trie (TST).
"""
The TST class represents an symbol table of key-value
pairs, with string keys and generic values.
It supports the usual put, get, contains,
delete, size, and is-empty methods.
It also provides character-based methods for finding the string
in the symbol table that is the longest prefix of a given prefix,
finding all strings in the symbol table that start with a given prefix,
and finding all strings in the symbol table that match a given pattern.
A symbol table implements the associative array abstraction:
when associating a value with a key that is already in the symbol table,
the convention is to replace the old value with the new value.
This class uses the convention that
values cannot be None setting the
value associated with a key to None is equivalent to deleting the key
from the symbol table.
This implementation uses a ternary search trie.
"""
class TST(object):
class Node:
def __init__(self):
self.c = None # character
self.left = None # left, middle and right subtries
self.mid = None
self.right = None
self.val = None # value associated with string
def __init__(self):
self.n = 0 # size
self.root = None # root of TST
# @return the number of key-value pairs in this symbol table
def size(self):
return self.n
def __len__(self):
return self.size()
# Does this symbol table contain the given key?
# @param key the key
# @return True if this symbol table contains key and
# False otherwise
# @raises ValueError if key is None
def contains(self, key):
if key is None:
raise ValueError(
"argument of contains is None"
) # TODO maybe get a specific exception, like IllegalArgumentException in Java
return self.get(key) is not None
# Returns the value associated with the given key.
# @param key the key
# @return the value associated with the given key if the key is in the symbol table
# and null if the key is not in the symbol table
# @raises ValueError if key is None
def get(self, key):
if key is None:
raise ValueError(
"calls get() with null argument"
) # TODO IllegalArgumentException?
if len(key) == 0:
raise ValueError(
"key must have length >=1"
) # TODO IllegalArgumentException?
x = self._get(self.root, key, 0)
if x is None:
return None
return x.val
# return subtrie corresponding to given key
def _get(self, x, key, d):
if x is None:
return None
if len(key) == 0:
raise ValueError("key nust have length >= 1")
c = key[d]
if c < x.c:
return self._get(x.left, key, d)
elif c > x.c:
return self._get(x.right, key, d)
elif d < len(key) - 1:
return self._get(x.mid, key, d + 1)
else:
return x
# Inserts the key-value pair into the symbol table, overwriting the old value
# with the new value if the key is already in the symbol table.
# If the value is None, this effectively deletes the key from the symbol table.
# @param key the key
# @param val the value
# @raises ValueError if key is None
def put(self, key, val):
if key is None:
raise ValueError(
"calls put() with null key"
) # TODO IllegalArgumentException
if not self.contains(key):
self.n += 1
self.root = self._put(self.root, key, val, 0)
def _put(self, x, key, val, d):
c = key[d]
if x is None:
x = self.Node()
x.c = c
if c < x.c:
x.left = self._put(x.left, key, val, d)
elif c > x.c:
x.right = self._put(x.right, key, val, d)
elif d < len(key) - 1:
x.mid = self._put(x.mid, key, val, d + 1)
else:
x.val = val
return x
# Returns the string in the symbol table that is the longest prefix of query,
# or None, if no such string.
# @param query the query string
# @return the string in the symbol table that is the longest prefix of query,
# or None if no such string
# @raises ValueError if query is None
def longest_prefix_of(self, query):
if query is None:
raise ValueError("calls longest_path_of() with None argument")
if len(query) == 0:
return None
length = 0
x = self.root
i = 0
while x is not None and i < len(query):
c = query[i]
if c < x.c:
x = x.left
elif c > x.c:
x = x.right
else:
i += 1
if x.val is not None:
length = i
x = x.mid
return query[0:length]
# Returns all keys in the symbol table as an Iterable.
# To iterate over all of the keys in the symbol table named st,
# use the foreach notation: for key in st.keys().
# @return all keys in the symbol table as an Iterable
def keys(self):
queue = Queue()
self._collect(self.root, "", queue)
return queue
# Returns all of the keys in the set that start with prefix.
# @param prefix the prefix
# @return all of the keys in the set that start with prefix,
# as an iterable
# @raises ValueError if prefix is None
def keys_with_prefix(self, prefix):
if prefix is None:
raise ValueError(
"calls keys_with_prefix with null argument"
) # TODO IllegalArgumentException
queue = Queue()
x = self._get(self.root, prefix, 0)
if x is None:
return queue
if x.val is not None:
queue.enqueue(prefix)
self._collect(x.mid, prefix, queue)
return queue
# all keys in subtrie rooted at x with given prefix
def _collect(self, x, prefix, queue):
if x is None:
return
self._collect(x.left, prefix, queue)
if x.val is not None:
queue.enqueue(prefix + str(x.c))
self._collect(x.mid, prefix + str(x.c), queue)
self._collect(x.right, prefix, queue)
# Returns all of the keys in the symbol table that match pattern,
# where . symbol is treated as a wildcard character.
# @param pattern the pattern
# @return all of the keys in the symbol table that match pattern,
# as an iterable, where . is treated as a wildcard character.
def keys_that_match(self, pattern):
queue = Queue()
self._collect_match(self.root, "", 0, pattern, queue)
return queue
def _collect_match(self, x, prefix, i, pattern, queue):
if x is None:
return
c = pattern[i]
if c == "." or c < x.c:
self._collect_match(x.left, prefix, i, pattern, queue)
if c == "." or c == x.c:
if i == len(pattern) - 1 and x.val is not None:
queue.enqueue(prefix + str(x.c))
if i < len(pattern) - 1:
self._collect_match(x.mid, prefix + x.c, i + 1, pattern, queue)
if c == "." or c > x.c:
self._collect_match(x.right, prefix, i, pattern, queue)
# * Unit tests the TST data type.
def test():
st = TST()
st.put("abc", 0)
keys = [k for k in st.keys()]
assert keys == ["abc"]
assert st.get("abc") == 0
st.put("a", 1)
st.put("b", 2)
st.put("c", 3)
# st.delete("abc")
# assert "abc" not in [k for k in st.keys()]
assert st.get("a") == 1
assert st.get("b") == 2
assert st.get("c") == 3
st.put("hello", 10)
assert st.contains("hello")
assert st.contains("not there") is False
st.put("he", 20)
assert st.longest_prefix_of("hell") == "he"
st.put("jello", 30)
q = st.keys_that_match(".e.l.")
assert q.size() == 2 and "jello" in q and "hello" in q
print("tests passed.")
if __name__ == "__main__":
test()
st = TST()
i = 0
# build symbol table from stdin
print("Insert keys (Ctrl-D to stop):")
while not stdio.isEmpty():
key = stdio.readString()
st.put(key, i)
i += 1
# print results
if st.size() < 100:
print('keys(""):')
for key in st.keys():
print("{} {}".format(key, st.get(key)))
print()
print('longestPrefixOf("shellsort"):')
print(st.longest_prefix_of("shellsort"))
print()
print('longestPrefixOf("shell"):')
print(st.longest_prefix_of("shell"))
print()
print('keysWithPrefix("shor"):')
for s in st.keys_with_prefix("shor"):
print(s)
print()
print('keysThatMatch(".he.l."):')
for s in st.keys_that_match(".he.l."):
print(s)
================================================
FILE: setup.cfg
================================================
[flake8]
ignore = E203, E266, E501, W503
max-line-length = 88
max-complexity = 18
select = B,C,E,F,W,T4
[isort]
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
use_parentheses=True
line_length = 88
[mypy]
files=.
exclude=itu
ignore_missing_imports=true
[tool:pytest]
testpaths=tests
[coverage:run]
source = itu
[coverage:report]
exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover
# Don't complain about missing debug-only code:
def __repr__
if self\.debug
# Don't complain if tests don't hit defensive assertion code:
raise AssertionError
raise NotImplementedError
# Don't complain if non-runnable code isn't run:
if 0:
if __name__ == .__main__.:
================================================
FILE: setup.py
================================================
from setuptools import find_packages, setup
setup(
name="itu.algs4",
version="0.2.5",
description='Python 3 port of the Java code in "Algorithms, 4th Edition" by Sedgewick and Wayne',
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
author="Algorithms group at ITU Copenhagen",
# author_email='',
url="https://github.com/itu-algorithms/itu.algs4/",
license="GNU General Public License v3 (GPLv3)",
packages=find_packages(exclude=["examples", "tests"]),
include_package_data=True,
extras_require={
"audio": ["numpy"],
"visual": ["pygame"],
"dev": [
"flake8",
"black",
"isort",
"pytest",
"pytest-cov",
"coveralls",
"mypy",
],
},
zip_safe=False,
platforms="any",
classifiers=[
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
"Operating System :: OS Independent",
"Natural Language :: English",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Topic :: Utilities",
"Intended Audience :: Developers",
"Intended Audience :: Education",
"Intended Audience :: Science/Research",
"Topic :: Software Development :: Libraries :: Python Modules",
],
)
================================================
FILE: tests/test_bst.py
================================================
import random
import unittest
from itu.algs4.errors.errors import NoSuchElementException
from itu.algs4.searching.bst import BST
class TestBSTMethods(unittest.TestCase):
def setUp(self):
self.bst = BST()
def test_empty(self):
self.assertTrue(self.bst.is_empty())
self.bst.put(0, 0)
self.assertFalse(self.bst.is_empty())
def test_size(self):
for i in range(10):
self.assertEqual(i, self.bst.size())
self.bst.put(str(i), i)
for i in range(10):
self.assertEqual(10, self.bst.size())
self.bst.put(str(i), i + 1) # key already there: no change in size
for i in reversed(range(10)):
self.assertEqual(i + 1, self.bst.size())
self.bst.delete(str(i))
self.assertEqual(0, self.bst.size())
def test_floor_and_ceiling(self):
self.bst.put(0, 0)
self.bst.put(2, 2)
self.assertEqual(0, self.bst.floor(0))
self.assertEqual(0, self.bst.floor(1))
self.assertEqual(2, self.bst.floor(2))
self.assertEqual(2, self.bst.floor(3))
self.assertEqual(0, self.bst.ceiling(-1))
self.assertEqual(0, self.bst.ceiling(0))
self.assertEqual(2, self.bst.ceiling(1))
self.assertEqual(2, self.bst.ceiling(2))
def test_rank_select(self):
for i in range(0, 2 ** 8 + 2, 2):
self.bst.put(i, i)
self.assertEqual(0, self.bst.min())
self.assertEqual(i, self.bst.max())
self.assertEqual(i, self.bst.select(i // 2))
self.assertEqual(i // 2, self.bst.rank(i))
def test_exceptions(self):
with self.assertRaises(NoSuchElementException):
self.bst.min()
with self.assertRaises(NoSuchElementException):
self.bst.max()
with self.assertRaises(NoSuchElementException):
self.bst.floor(0)
self.bst.put(0, 0)
with self.assertRaises(NoSuchElementException):
self.bst.floor(-1)
class LargerBSTMethods(unittest.TestCase):
L = [1, 3, 6, 7, 10, 13, 16]
def setUp(self):
self.bst = BST()
for x in self.L:
self.bst.put(x, x)
def test_put_and_get(self):
st = self.bst
for x in st.keys():
self.assertEqual(st.get(x), x)
for x in st.keys():
st.put(x, st.get(x) - 1)
for x in st.keys():
self.assertEqual(st.get(x), x - 1)
self.assertIsNone(st.get(2))
st.put(2, 2)
self.assertEqual(2, st.get(2))
def test_keys(self):
i = 0
for x in self.bst.keys():
self.assertEqual(x, self.L[i])
i += 1
def test_min(self):
self.assertEqual(self.bst.min(), min(self.L))
def test_max(self):
self.assertEqual(self.bst.max(), max(self.L))
def test_delete_min(self):
self.bst.delete_min()
self.assertEqual(self.bst.min(), min(self.L[1:]))
def test_delete_max(self):
self.bst.delete_max()
self.assertEqual(self.bst.max(), max(self.L[:-1]))
def test_range(self):
R = [6, 7, 10, 13]
i = 0
for x in self.bst.range_keys(5, 13):
self.assertEqual(x, R[i])
i += 1
class HugeBSTMethods(unittest.TestCase):
def setUp(self):
random.seed(0)
self.L = random.sample(range(10 ** 6), 10 ** 4)
self.S = sorted(self.L)
self.bst = BST()
for x in self.L:
self.bst.put(x, x)
def test_put_and_get(self):
st = self.bst
for x in st.keys():
self.assertEqual(st.get(x), x)
for x in st.keys():
st.put(x, st.get(x) - 1)
for x in st.keys():
self.assertEqual(st.get(x), x - 1)
def test_keys(self):
i = 0
for x in self.bst.keys():
self.assertEqual(x, self.S[i])
i += 1
def test_min(self):
self.assertEqual(self.bst.min(), min(self.L))
def test_max(self):
self.assertEqual(self.bst.max(), max(self.L))
def test_delete_min(self):
self.bst.delete_min()
self.assertEqual(self.bst.min(), min(self.S[1:]))
def test_delete_max(self):
self.bst.delete_max()
self.assertEqual(self.bst.max(), max(self.S[:-1]))
def test_min_priority_queue(self):
i = 0
while not self.bst.is_empty():
self.assertEqual(self.S[i], self.bst.min())
self.bst.delete_min()
i += 1
def test_max_priority_queue(self):
i = len(self.S) - 1
while not self.bst.is_empty():
self.assertEqual(self.S[i], self.bst.max())
self.bst.delete_max()
i -= 1
================================================
FILE: tests/test_red_black_bst.py
================================================
import random
import unittest
from itu.algs4.errors.errors import NoSuchElementException
from itu.algs4.searching.red_black_bst import RedBlackBST
class TestRedBlackBSTMethods(unittest.TestCase):
def setUp(self):
self.st = RedBlackBST()
def test_empty(self):
self.assertTrue(self.st.is_empty())
self.st.put("spam", 0)
self.assertFalse(self.st.is_empty())
def test_size(self):
for i in range(10):
self.assertEqual(i, self.st.size())
self.st.put(str(i), i)
for i in range(10):
self.assertEqual(10, self.st.size())
self.st.put(str(i), i + 1) # key already there: no change in size
for i in reversed(range(10)):
self.assertEqual(i + 1, self.st.size())
self.st.delete(str(i))
self.assertEqual(0, self.st.size())
def test_rank_select(self):
for i in range(0, 2 ** 8 + 2, 2):
self.st.put(i, i)
self.assertEqual(0, self.st.min())
self.assertEqual(i, self.st.max())
self.assertEqual(i, self.st.select(i // 2))
self.assertEqual(i // 2, self.st.rank(i))
def test_floor_and_ceiling(self):
self.st.put(0, 0)
self.st.put(2, 2)
self.assertEqual(0, self.st.floor(0))
self.assertEqual(0, self.st.floor(1))
self.assertEqual(2, self.st.floor(2))
self.assertEqual(2, self.st.floor(3))
self.assertEqual(0, self.st.ceiling(-1))
self.assertEqual(0, self.st.ceiling(0))
self.assertEqual(2, self.st.ceiling(1))
self.assertEqual(2, self.st.ceiling(2))
def test_exceptions(self):
with self.assertRaises(NoSuchElementException):
self.st.min()
with self.assertRaises(NoSuchElementException):
self.st.max()
with self.assertRaises(NoSuchElementException):
self.st.floor(0)
with self.assertRaises(NoSuchElementException):
self.st.ceiling(0)
self.st.put(0, 0)
with self.assertRaises(NoSuchElementException):
self.st.floor(-1)
with self.assertRaises(NoSuchElementException):
self.st.ceiling(1)
class LargerRedBlackBSTMethods(unittest.TestCase):
L = [1, 3, 6, 7, 10, 13, 16]
def setUp(self):
self.st = RedBlackBST()
for x in self.L:
self.st.put(x, x)
def test_put_and_get(self):
st = self.st
for x in st.keys():
self.assertEqual(st.get(x), x)
for x in st.keys():
st.put(x, st.get(x) - 1)
for x in st.keys():
self.assertEqual(st.get(x), x - 1)
self.assertIsNone(st.get(2))
st.put(2, 2)
self.assertEqual(2, st.get(2))
def test_keys(self):
i = 0
for k in self.st.keys():
self.assertEqual(k, self.L[i])
i += 1
def test_min(self):
self.assertEqual(self.st.min(), min(self.L))
def test_max(self):
self.assertEqual(self.st.max(), max(self.L))
def test_delete_min(self):
self.st.delete_min()
self.assertEqual(self.st.min(), min(self.L[1:]))
def test_delete_max(self):
self.st.delete_max()
self.assertEqual(self.st.max(), max(self.L[:-1]))
def test_range(self):
T = [6, 7, 10, 13]
i = 0
for k in self.st.keys_range(5, 13):
self.assertEqual(k, T[i])
i += 1
class HugeRedBlackBSTMethods(unittest.TestCase):
def setUp(self):
random.seed(0)
self.L = random.sample(range(10 ** 6), 10 ** 4)
self.S = sorted(self.L)
self.st = RedBlackBST()
for x in self.L:
self.st.put(x, x)
def test_put_and_get(self):
st = self.st
for x in st.keys():
self.assertEqual(st.get(x), x)
for x in st.keys():
st.put(x, st.get(x) - 1)
for x in st.keys():
self.assertEqual(st.get(x), x - 1)
def test_keys(self):
i = 0
for k in self.st.keys():
self.assertEqual(k, self.S[i])
i += 1
def test_min(self):
self.assertEqual(self.st.min(), min(self.L))
def test_max(self):
self.assertEqual(self.st.max(), max(self.L))
def test_delete_min(self):
self.st.delete_min()
self.assertEqual(self.st.min(), min(self.S[1:]))
def test_delete_max(self):
self.st.delete_max()
self.assertEqual(self.st.max(), max(self.S[:-1]))
def test_min_priority_queue(self):
i = 0
while not self.st.is_empty():
self.assertEqual(self.S[i], self.st.min())
self.st.delete_min()
i += 1
def test_max_priority_queue(self):
i = len(self.S) - 1
while not self.st.is_empty():
self.assertEqual(self.S[i], self.st.max())
self.st.delete_max()
i -= 1
def test_flights(self):
self.st = RedBlackBST()
schedule = [
["09:00:00", "Chicago"],
["09:00:03", "Phoenix"],
["09:00:13", "Houston"],
["09:00:59", "Chicago"],
["09:01:10", "Houston"],
["09:03:13", "Chicago"],
["09:10:11", "Seattle"],
["09:10:25", "Seattle"],
["09:14:25", "Phoenix"],
["09:19:32", "Chicago"],
["09:19:46", "Chicago"],
["09:21:05", "Chicago"],
["09:22:43", "Seattle"],
["09:22:54", "Seattle"],
["09:25:52", "Chicago"],
["09:35:21", "Chicago"],
["09:36:14", "Seattle"],
["09:37:44", "Phoenix"],
]
for time, city in schedule:
self.st.put(time, city)
for time, city in schedule:
self.assertEqual(city, self.st.get(time))
self.assertEqual(len(self.st.keys()), len(schedule))
================================================
FILE: tests/test_stack.py
================================================
import random
import pytest
from itu.algs4.fundamentals.stack import FixedCapacityStack, ResizingArrayStack, Stack
@pytest.mark.parametrize(
"stack",
[
FixedCapacityStack(0),
FixedCapacityStack(1),
FixedCapacityStack(7),
ResizingArrayStack(),
Stack(),
],
)
def test_is_empty(stack):
assert stack.is_empty()
@pytest.mark.parametrize(
"stack",
[FixedCapacityStack(1), FixedCapacityStack(7), ResizingArrayStack(), Stack()],
)
def test_push_pop_once(stack):
stack.push(83)
assert not stack.is_empty()
assert stack.pop() == 83
assert stack.is_empty()
@pytest.mark.parametrize("capacity", list(range(10)))
def test_error_beyond_capacity(capacity):
stack = FixedCapacityStack(capacity)
for i in range(capacity):
stack.push(i)
try:
stack.push(99)
assert False
except IndexError:
pass
@pytest.mark.parametrize(
"stack", [FixedCapacityStack(100), ResizingArrayStack(), Stack()]
)
@pytest.mark.parametrize("seed", [3928, 1928, 39211, 419901])
def test_random_pushpop_sequence(stack, seed):
random.seed(seed)
L = list(range(100))
random.shuffle(L)
for i in L:
stack.push(i)
L.reverse()
for i in L:
assert stack.pop() == i
assert stack.is_empty()
================================================
FILE: tests/test_symbol_tables.py
================================================
# import random
#
import pytest
from itu.algs4.searching.binary_search_st import BinarySearchST
from itu.algs4.searching.bst import BST
from itu.algs4.searching.linear_probing_hst import LinearProbingHashST
from itu.algs4.searching.red_black_bst import RedBlackBST
from itu.algs4.searching.seperate_chaining_hst import SeparateChainingHashST
from itu.algs4.searching.sequential_search_st import SequentialSearchST
from itu.algs4.searching.st import ST
ST_IMPLEMENTATIONS = [
BinarySearchST,
BST,
LinearProbingHashST,
RedBlackBST,
SeparateChainingHashST,
SequentialSearchST,
ST,
]
@pytest.mark.parametrize(
"st",
[constructor() for constructor in ST_IMPLEMENTATIONS],
)
def test_is_empty(st):
assert st.is_empty()
@pytest.mark.parametrize(
"st",
[constructor() for constructor in ST_IMPLEMENTATIONS],
)
def test_delete(st):
st.put("key1", "val1")
st.put("key2", "val2")
st.put("key3", "val3")
st.delete("key3")
st.delete("key2")
st.delete("key1")
assert st.is_empty()
@pytest.mark.parametrize(
"st",
[constructor() for constructor in ST_IMPLEMENTATIONS],
)
def old_tests(st):
st.put("one", 1)
assert ["one"] == list(st.keys())
assert st.get("one") == 1
assert st.contains("one")
st.delete("one")
assert st.is_empty()
st.put("aaa", 1)
st.put("bbb", 2)
st.put("ccc", 3)
st.put("ddd", 4)
st.put("eee", 5)
if st.ceiling:
assert st.ceiling("ccc") == "ccc"
assert st.ceiling("dad") == "ddd"
assert st.floor("ccc") == "ccc"
assert st.floor("dad") == "ccc"
for k in st:
assert k in st.keys()
assert st.min() == "aaa"
assert st.max() == "eee"