Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a weighted digraph with an adjacency map #14

Open
11 tasks
mwermelinger opened this issue Jan 7, 2024 · 1 comment
Open
11 tasks

Implement a weighted digraph with an adjacency map #14

mwermelinger opened this issue Jan 7, 2024 · 1 comment
Labels
effort: high These issues require the most work priority: medium type: feature New feature

Comments

@mwermelinger
Copy link
Member

You may copy and adapt code from Section 18.2.2 of the M269 book. You may divide the tasks below with others, to reduce workload.

Create a file paddles/digraph.py with a class AdjacencyMapWeightedDiGraph:

  • The docstring of the module explains the Digraph ADT.
  • Write inspector methods to
    • return the number of nodes and of edges
    • check if a given node or edge exists
    • return the in- and out-degree of a node (raise ValueError if the node doesn't exist)
    • return the in- and out-neighbours of a node (raise ValueError if the node doesn't exist)
    • return the weight of a given edge (raise ValueError if the edge – i.e. either node – doesn't exist)
  • Write modifier methods to
    • add a node (raise ValueError if the node already exists)
    • remove a node and all its edges (raise ValueError if the node to remove doesn't exist)
    • add an edge (raise ValueError if the edge is between the same node – a loop)
    • remove an edge (raise ValueError if the edge to remove doesn't exist)

Create a file tests/test_digraph.py:

  • Write a test for each modifier method, using the inspector methods to check the behaviour.
@mwermelinger mwermelinger added effort: high These issues require the most work priority: medium labels Jan 7, 2024
@mwermelinger
Copy link
Member Author

The add edge method may take an optional weight with default value 1 so that the class can also be used as an unweighted graph. However, pedagogically it may be better to have a simpler superclass for unweighted graphs. The exact class hierarchy for un/weighted di/graphs requires a bit of thinking. It would be good to allow extension in future to non-simple graphs.

In networkx, digraph is a subclass of undirected and stores both the out- and in-neighbours. Arbitrary attributes can be attached to a graph, a node or an edge. Weight is just an attribute like any other. Both classes allow loops. There are two separate classes for graphs with multi-edges.

@mwermelinger mwermelinger added the type: feature New feature label Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort: high These issues require the most work priority: medium type: feature New feature
Projects
None yet
Development

No branches or pull requests

1 participant