This project involves understanding and implementing fundamental graph algorithms using the networkX
library in Python. The tasks include creating and analyzing graphs, implementing DFS and BFS, and applying Dijkstra's algorithm.
Description:
- Create a graph to model a real-world network. This can be a transportation network (e.g., city roads), a social network (e.g., friendships), or an internet topology.
- Steps:
- Graph Construction: Use the
networkX
library to build the graph. - Visualization: Render a visual representation of the graph to understand its structure.
- Analysis: Examine key characteristics of the graph, including:
- Number of vertices (nodes)
- Number of edges (connections)
- Degree of vertices (number of connections each node has)
- Graph Construction: Use the
Description:
- Implement two fundamental search algorithms, DFS and BFS, to explore paths within the graph created in Task 1.
- Steps:
- DFS Implementation: Write a program to perform depth-first search and find paths in the graph.
- BFS Implementation: Write a program to perform breadth-first search and find paths in the graph.
- Comparison: Compare the results from both algorithms. Discuss:
- How the paths differ between DFS and BFS
- Why these differences occur based on the nature of each algorithm (depth-first vs. breadth-first)
Description:
- Apply Dijkstra's algorithm to find the shortest paths in the graph.
- Steps:
- Add Weights: Assign weights (e.g., distances or costs) to the edges of the graph.
- Algorithm Implementation: Implement Dijkstra's algorithm to compute the shortest path between all pairs of vertices.
- Shortest Path Calculation: Output the shortest path lengths and routes for each pair of vertices.
The provided code creates an undirected graph representing the public transport routes in Lviv with six stations:
- Головний вокзал
- Площа Ринок
- Опера
- Високий замок
- Політехніка
- Медичний університет
- Головний вокзал - Площа Ринок
- Площа Ринок - Опера
- Опера - Високий замок
- Високий замок - Політехніка
- Політехніка - Медичний університет
- Медичний університет - Головний вокзал
- Головний вокзал - Опера
- Площа Ринок - Високий замок
The graph visualization is created using NetworkX and Matplotlib, showing nodes and edges labeled with their respective names and edge numbers.
- Total number of nodes: 6
- Total number of edges: 8
Station | Degree |
---|---|
Головний вокзал | 3 |
Площа Ринок | 3 |
Опера | 3 |
Високий замок | 3 |
Політехніка | 2 |
Медичний університет | 2 |
- Average Degree: The average number of connections per node (average degree) is approximately 2.67, indicating a moderate level of connectivity in the network.
- Key Transport Hubs:
- The stations Головний вокзал, Площа Ринок, Опера, and Високий замок each have 3 connections, highlighting their importance as key transport hubs within the network.
- The stations Політехніка and Медичний університет each have 2 connections, which is typical for nodes in a network with a moderate level of connectivity.
The analysis provides insights into the structure and connectivity of the public transport network in Lviv, with certain stations identified as key hubs based on their higher degree of connectivity.
Based on the results, we can conclude that the Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms produce different traversal paths for the given graph.
The DFS traversal path is: ['Головний вокзал', 'Опера', 'Високий замок', 'Площа Ринок', 'Політехніка', 'Медичний університет']
The BFS traversal path is: ['Головний вокзал', 'Площа Ринок', 'Медичний університет', 'Опера', 'Високий замок', 'Політехніка']
These results demonstrate the fundamental differences between DFS and BFS algorithms. DFS explores the graph depth-first, visiting as far as possible along each branch before backtracking, whereas BFS explores the graph level by level, visiting all nodes at a given depth before moving on to the next level.
The code includes an implementation of Dijkstra's algorithm to find the shortest paths from each station to every other station in the graph.
The shortest distances between all pairs of vertices calculated using Dijkstra's algorithm are as follows:
From \ To | Головний вокзал | Площа Ринок | Опера | Високий замок | Політехніка | Медичний університет |
---|---|---|---|---|---|---|
Головний вокзал | 0 | 2 | 5 | 9 | 14 | 7 |
Площа Ринок | 2 | 0 | 3 | 7 | 12 | 9 |
Опера | 5 | 3 | 0 | 4 | 9 | 12 |
Високий замок | 9 | 7 | 4 | 0 | 5 | 11 |
Політехніка | 14 | 12 | 9 | 5 | 0 | 6 |
Медичний університет | 7 | 9 | 12 | 11 | 6 | 0 |
The visualization of the graph and the results of Dijkstra's algorithm demonstrate the structure and connectivity of the public transport routes in Lviv.
-
Graph Visualization: The graph visualization clearly shows the stations and the routes between them, with edge weights representing the distances. This helps in understanding the layout of the network and the relative distances between stations.
-
Shortest Distances: The shortest distances between all pairs of vertices, calculated using Dijkstra's algorithm, provide a clear understanding of the most efficient paths between any two stations in the network. These shortest paths can be useful in various applications, such as:
- Finding the most efficient route between two locations in a transportation network.
- Optimizing travel times and distances in route planning.
- Analyzing the connectivity and efficiency of the transport network.
Overall, the successful calculation of the shortest distances between all pairs of vertices is a crucial step in graph analysis and can have significant implications for transportation planning and optimization.