Graph Implementation In Python [PART 2] By albro
Let's look at some different examples of graph implementations in Python using dictionaries.
In the first example, I want to implement the undirected graph drawn in the box below using Python dictionaries.
0
/ \
1---2
\ /
3
The above graph can be easily displayed using dictionaries. In the box below, I have implemented the above graph definition code with both edges and vertices dictionaries.
vertices = {
0: [1, 2],
1: [0, 2, 3],
2: [0, 1, 3],
3: [1, 2]
}
edges = {
0: {1, 2},
1: {0, 2, 3},
2: {0, 1, 3},
3: {1, 2}
}
In this example, I decide to implement directed graph maintenance code using Python dictionaries. To solve this problem, I use the arbitrary graph drawn in the box below.
0 --> 1 --> 2 --> 3
| | |
v v v
4 5 --> 6
Dictionaries are among the most widely used structures in the Python programming language. Graph definition is one of the smallest capabilities of this structure. With the help of the dictionaries implemented below, I have displayed the above graph.
vertices = {
0: [1, 4],
1: [2],
2: [3, 5],
3: [6],
4: [],
5: [6],
6: []
}
edges = {
0: {1, 4},
1: {2},
2: {3, 5},
3: {6},
4: set(),
5: {6},
6: set()
}
A weighted graph is a graph in which each edge has a specific weight. The general shape of the graph we want is the same as the first example in the post that I have shown below.
0
/ \
1---2
\ /
3
In this problem, the weights of all edges are determined as follows.
- The weight of the edge between vertices 0 and 1 is equal to 4.
- The weight of the edge between vertices 0 and 2 is equal to 5.
- The weight of the edge between vertices 1 and 2 is equal to 3.
- The weight of the edge between vertices 1 and 3 is equal to 2.
- The weight of the edge between vertices 2 and 3 is equal to 6.
Now in the box below, I have implemented the above weighted graph with the help of dictionaries.
vertices = {
0: [(1, 4), (2, 5)],
1: [(0, 4), (2, 3), (3, 2)],
2: [(0, 5), (1, 3), (3, 6)],
3: [(1, 2), (2, 6)]
}
edges = {
0: {(1, 4), (2, 5)},
1: {(0, 4), (2, 3), (3, 2)},
2: {(0, 5), (1, 3), (3, 6)},
3: {(1, 2), (2, 6)}
}
In the code above, the values of the keys in the vertices dictionary are tuples in Python. In both dictionaries, the keys represent all the vertices of the graph. In these tuples, the first element shows the adjacent vertex and the second element shows the weight of the edge connecting them. The values of each key in the edges dictionary are also displayed in the form of tuples. In these tuples, the first element shows the adjacent vertex and the second element is the weight of the edge connecting these vertices together.
Using a dictionary to display graph vertices and edges in Python is a flexible and optimal method in terms of memory consumption. In this method to display graphs, operations related to removing and adding vertices and edges are done simply. However, traversing graphs in this method is slower than other methods such as adjacency lists.
Leave Graph Implementation In Python [PART 2] By albro to:
Read more #hive-169321 posts
Best Posts From albro
We have not curated any of albro's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From albro
- An Introduction to MongoDB
- Unlocking the Power of Swift: An Introduction to Programming Structure, Constants, and Variables
- Mastering NestJS: From Installation to Coding Best Practices
- Understanding Variables in Different Programming Languages
- Mastering Julia: Variables, Conditionals, Loops, and Practical Uses
- LISP Programming By albro
- Graph implementation with NetworkX in Python
- Graph Implementation In Python [PART 2] By albro
- Graph Implementation In Python [PART 1] By albro
- Make Your Own Game By albro
- Hive Small Codes: Receive incoming transaction information from an account
- Powershell programming
- Network Programming & Tricks
- Unknown & Never in TypeScript
- Function Type & Callback In TypeScript
- Alias & Return Type In TypeScript
- Union Type & Literal Type By albro
- enum & any By albro
- Array & Tuple Type By albro
- Object Type In TypeScript By albro