Course Programming Assignments Repository
This repository provides a complete testing and execution environment for graph algorithm programming assignments. Students implement core algorithms while the infrastructure handles graph loading, parsing, and testing.
tasks/task1_bfs.py- Implement the maximal blue nodes on shortest paths algorithmtasks/task2_mst.py- Implement MST and 2nd MST algorithmstasks/task3_choice.py- Implement your chosen algorithm
graph/graph.py- Graph class (instructor-provided)graph/loaders.py- Graph loading utilitiesexamples/- Reference implementations (read-only)test_task1.py,test_task2.py,test_task3.py- Test scripts
Violation of these rules may result in assignment rejection.
git clone <repository-url>
cd graph-algorithms-testbenchThis project uses only Python standard library. No additional packages need to be installed.
Run the example files to ensure everything works:
python examples/bfs_example.py
python examples/dfs_example.pyFile: tasks/task1_bfs.py
Implement "max_blue_path(graph, s, t)" that returns the maximum number of "blue" vertices
Test: python test_task1.py graphfile s t
File: tasks/task2_mst.py
Implement two functions:
- MST(graph) -- returns the edges that form the MST
- second_best_ST(graph)` - Find the second-best spanning tree, i.e., the set of edges that form a spanning tree with weight > than MST, but smallest weight possible for all spanning trees.
You may implement Union-Find in this file or use examples/union_find_example.py, if you think it is necessary
Test: python test_task2.py graphfile
Implement one of two functions:
- centralities(graph) -- returns a dictionary of the vertices of graph where the values are betweenness centralities
- communities(graph) -- returns an iterable of iterables (sets) such that each vertex belongs to one set, these sets are communities
File: tasks/task3_choice.py
Test (centrality): python test_task3.py -A graphfile
Test (community): python test_task3.py -B graphfile
Each task has a standalone test script that takes a graph file as input. For the community detection, we do not report correctenss, instead we report on overlap metric of the sets produced by other methods; there is no strict correctness in that task.
All tasks use the same Graph class. Here's how to use it:
from graph import Graph
# Undirected, unweighted graph
g = Graph(directed=False, weighted=False)
# Directed, weighted graph
g = Graph(directed=True, weighted=True)g.add_vertex("A")
g.add_edge("A", "B") # Unweighted (weight = 1.0)
g.add_edge("A", "C", 5.0) # Weightedvertices = g.vertices() # List of all vertices
neighbors = g.neighbors("A") # List of A's neighbors
edges = g.edges() # List of (u, v, weight) tuples
weight = g.weight("A", "B") # Get edge weight
has_edge = g.has_edge("A", "B") # Check if edge existsfrom graph import load_graph
g = load_graph("data/small_graph.json")
g = load_graph("data/weighted_graph.json")See examples/ for complete usage examples.
The examples/ directory contains reference implementations:
bfs_example.py- Breadth-first searchdfs_example.py- Depth-first searchdijkstra_example.py- Dijkstra's algorithmunion_find_example.py- Union-Find data structure
These are for learning purposes. You may read and understand them, but:
- DO NOT copy-paste code directly
- DO use them to understand the Graph API
- DO use Union-Find from examples in Task 2 if needed
Submit ONLY the following files:
tasks/task1_bfs.pyfor the first assignmenttasks/task2_mst.pyfor the second assignmenttasks/task3_choice.pyfor the third assignment