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.