OOPS

Assignment - 5

Objective

Take a map provided as a graph & display all the subtrees of connected points.

Programming Language & Compilation

# install networkx (https://github.com/networkx/networkx)
$ python3 -m pip install networkx
# install https://github.com/matplotlib/matplotlib
$ python3 -m pip install matplotlib
# run the code below
$ python3 main.py

Source Code

import networkx as nx
import matplotlib.pyplot as plt

# A `Location` object describes a location on the map, which holds:
# 1. Name of that location.
# 2. Other locations that it is connected to.
#
# This data is then later used to create a graph.
#
class Location:
    name: str
    connections: list[str]

    def __init__(self, name: str, connections: list[str]):
        self.name = name
        self.connections = connections


# Primary class to visualize the given map.
# This class contains the main logic to create the graph and visualize it.
# Takes in a list of `Location` objects in its parameterized constructor.
class MapVisualizer:
    # Used for map visualization.
    visual: list[list[str]] = []

    def __init__(self, locations: list[Location]):
        # Iterate over the nodes, provided as `Location` objects.
        for a in locations:
            for b in a.connections:
                self.add_edge(a.name, b)

    # A helper method to connect to location nodes.
    def add_edge(self, a: str, b: str):
        self.visual.append([a, b])

    # Display the created map as graph.
    def visualize(self):
        graph = nx.Graph()
        graph.add_edges_from(self.visual)

        nx.draw(
            graph,
            with_labels=True,
            node_color="#81d4fa",
            edge_color="#aaaaaa",
            node_size=1000,
        )
        plt.show()


# Entry point of the program.
if __name__ == "__main__":
    # List of location objects that should be represnted.
    locations = [
        # Location("name of location", ["places", "it", "is", "connected", "to"])
        Location(
            "Haldwani",
            ["Lal Kuan"],
        ),
        Location(
            "Lal Kuan",
            ["Haldwani", "Nagla"],
        ),
        Location(
            "College of Fisheries",
            ["Nagla", "Stevenson Stadium"],
        ),
        Location(
            "Stevenson Stadium",
            ["College of Fisheries", "Tagore Bhawan"],
        ),
        Location(
            "Tagore Bhawan",
            ["Stevenson Stadium", "Patel Bhawan", "College of Technology"],
        ),
        Location(
            "Patel Bhawan",
            ["Tagore Bhawan", "Silver Jubliee Bhawan", "College of Technology"],
        ),
        Location(
            "Silver Jubliee Bhawan",
            ["Patel Bhawan", "Visvesvaraya Bhawan"],
        ),
        Location(
            "Visvesvaraya Bhawan",
            [
                "Silver Jubliee Bhawan",
                "Badi Market",
                "Chhoti Market",
                "College of Technology",
            ],
        ),
        Location(
            "Chhoti Market",
            ["Visvesvaraya Bhawan"],
        ),
        Location(
            "Badi Market",
            ["Visvesvaraya Bhawan", "Registrar Office"],
        ),
        Location(
            "Registrar Office",
            ["Library", "Badi Market"],
        ),
        Location(
            "Library",
            ["College of Technology", "Registrar Office"],
        ),
        Location(
            "TIC",
            ["Patel Bhawan", "Silver Jubliee Bhawan"],
        ),
        Location(
            "University Hospital",
            ["Badi Market"],
        ),
    ]
    # Create new object instance & pass the location object references.
    visualizer = MapVisualizer(locations)
    visualizer.visualize()

Output

right-click & select “Open image in new tab” for larger image.

abdsaf

In this example, all the locations are connected, thus a single large subtree is generated.