3 Ways To Represent Graphs in Python | Graph Theory With Python #2

  Рет қаралды 35,017

David Amos

David Amos

Күн бұрын

Пікірлер
@emmanuelonah4596
@emmanuelonah4596 Жыл бұрын
The best video I have seen so far on graph representation in Python. Thank you!
@DavidAmos
@DavidAmos Жыл бұрын
Glad you enjoyed it!
@ScandGeek
@ScandGeek 3 жыл бұрын
Great video! Regarding the adjacency matrix: for undirected graphs there is a problem that loops will be recorded twice. For this you should add a condition which checks if node1 == node2.
@DavidAmos
@DavidAmos 3 жыл бұрын
Absolutely true. I will address this in a future video when I deal with more graphs with loops. I'm also going to revisit the adjacency matrix when I talk about graphs with weighted edges. Thanks for watching, and I'm glad you liked it!
@mahbuburrahmanturzo8712
@mahbuburrahmanturzo8712 2 жыл бұрын
Clear voice, good & detailed explanation! This tutorial is a gem!
@soumya0307
@soumya0307 2 жыл бұрын
You are the reason I just started fascinating about graph theory ❤
@Twoman-bodyweight-journey
@Twoman-bodyweight-journey 2 жыл бұрын
Thank you for your smooth explanations. 👌👌
@InfiniteQuest86
@InfiniteQuest86 2 жыл бұрын
This is nice. It's been a while since I've seen graph theory, but I could have sworn the adjacency matrix of a directed graph had negative entries for edges going away from the vertex. Is that just for specific use cases?
@DavidAmos
@DavidAmos 2 жыл бұрын
Glad you liked it! There are indeed several different ways to define the adjacency matrix, and many are application dependent. I went with what I think is the most common. As far as -1s in the matrix, you might be thinking instead of Laplacian (en.m.wikipedia.org/wiki/Laplacian_matrix) or some weighted version of the adjacency matrix.
@InfiniteQuest86
@InfiniteQuest86 2 жыл бұрын
@@DavidAmos I figured it out! It's called the incidence matrix: en.m.wikipedia.org/wiki/File:Incidence_matrix_-_directed_graph.svg . Which interestingly can calculate the Laplacian L = I(G)*I(G)^t.
@cybercor3
@cybercor3 Жыл бұрын
Very clear and detailed explanation. Thank you!!
@kelechiegbosi6531
@kelechiegbosi6531 3 жыл бұрын
Educative Video. Thanks for sharing.
@hamzashariq9057
@hamzashariq9057 2 жыл бұрын
for adjacency list,if we want to add the weight of nodes how can we do that?
@hericklenin
@hericklenin 2 жыл бұрын
This is a very good presentation, thanks so much.
@watcherofvideoswasteroftim5788
@watcherofvideoswasteroftim5788 2 жыл бұрын
Super helpful, thanks!
@speakbaskar9620
@speakbaskar9620 Жыл бұрын
def radio_labeling(order, diameter): labels = [0] * order # Assign label 1 to vertex 0 labels[0] = 1 # Assign label 2 to the neighbors of vertex 0 for i in range(1, min(diameter + 1, order)): labels[i] = 2 # Assign labels to the remaining vertices for i in range(diameter + 1, order): # Check the labels of neighbors used_labels = set() for neighbor in range(i - diameter, i): used_labels.add(labels[neighbor]) # Find the smallest unused label j = 1 while j in used_labels: j += 1 # Assign the label to the current vertex labels[i] = j return labels # Example usage order = 10 # Number of vertices in the graph diameter = 3 # Diameter of the graph labels = radio_labeling(order, diameter) print(labels)
@oanafocsa148
@oanafocsa148 4 ай бұрын
This is great! Thank you very much!
@jamshaidmushtaq1811
@jamshaidmushtaq1811 2 жыл бұрын
What if nodes are alphabets like 'A' or 'B'?
@DavidAmos
@DavidAmos 2 жыл бұрын
In that case you could map nodes to integers using something like dict(enumerate(nodes)) if you want the keys to be the numbers or dict(zip(nodes, range(len(nodes)))) to get the nodes as keys. That way you can build an adjacency matrix using the integers and then map them back to the node names as needed.
@worldentropy
@worldentropy 2 жыл бұрын
Great work. How would you extend this concept to graphs in the 3D plain (e.g. a cuboid or 3D Torus) ? Thanks again.
@speakbaskar9620
@speakbaskar9620 Жыл бұрын
Thank you for helping your video sir. Can you send me radio labeling algorithm using python ?
@amirsharapov5556
@amirsharapov5556 2 жыл бұрын
Amazing video!
@DavidAmos
@DavidAmos 2 жыл бұрын
Thanks! Glad you liked it 🙂
@gradsys5482
@gradsys5482 Жыл бұрын
If nodes =[1,2,3,4] we will get an index error. You need to modify the adjacency _matrix function as follow: adj[graph.nodes.index(node1)][graph.nodes.index(node2)] To work with node 's index
@soorajparakkattil2039
@soorajparakkattil2039 3 жыл бұрын
Great video.
@DavidAmos
@DavidAmos 3 жыл бұрын
Thanks!
@andrejznamenacek8763
@andrejznamenacek8763 2 жыл бұрын
What a shame there si not a way how to determine, whether we can get somehow through the edges from node x to node y.
@joaovaleriodesouzaneto8038
@joaovaleriodesouzaneto8038 Жыл бұрын
Thanks, very very very goood!
@miv_cleric
@miv_cleric 3 жыл бұрын
+1 for namedtuple
@DavidAmos
@DavidAmos 3 жыл бұрын
One of my favorites in the standard library.
@mmokhtabad
@mmokhtabad Жыл бұрын
Nice, only if it was louder 😊
@arnantaroy7468
@arnantaroy7468 3 жыл бұрын
for me it says indices must be integers or slice, not list
@DavidAmos
@DavidAmos 3 жыл бұрын
Hey there! Could you share which line of code you're seeing this error on? I'm happy to help sort it out 🙂
@arnantaroy7468
@arnantaroy7468 3 жыл бұрын
@@DavidAmos hi thanks for replying, My code takes input of Edges and puts it in a list like this edges = [ [5,6], [2,3], [3,6] ] where the first value is the parent node and second value is the child. When I put this in graph adjacent list it works but because I need to perform operations I need to use adjacent matrix and when I try to do it it says indices just be integers or slice, not list
@DavidAmos
@DavidAmos 3 жыл бұрын
Hey @Arnanta Roy. Hmmmm... that's frustrating. Are you using the code for the adjacency matrix from this video? Just some thoughts: the input for the adjacency_matrix() function in the video needs to be the Graph data structure that I also defined in this video. I'm not sure of the best way to post code in KZbin comments. If you'd like, you can open an issue on the GitHub repository for the video series with all of your code (link here 👉github.com/somacdivad/graph-theory-with-python). That way I can take a look at your code and see if I can help you out!
@arnantaroy7468
@arnantaroy7468 3 жыл бұрын
@@DavidAmos #!/bin/python3 import math import os import random import re import sys from collections import namedtuple # # Complete the 'similarPair' function below. # # The function is expected to return an INTEGER. # The function accepts following parameters: # 1. INTEGER n # 2. INTEGER k # 3. 2D_INTEGER_ARRAY edges # nodes = [] straightlist = [] Graph = namedtuple("Graph", ["nodes", "edges"]) def similarPair(n, k, edges): for elem in edges: straightlist.extend(elem) node = straightlist[0] dfs(nodes, straightlist, node) print(nodes) def dfs(visited, edges, node): if node not in visited: nodes.append(node) for i in edges: dfs(nodes, edges, i) def adjacency_matrix(graph): """ Returns the adjacency matrix of the graph. Assumes that graph.node is equivalent to range(len(graph.nodes)) """ adj = [[0 for node in graph.nodes] for node in graph.nodes] array_length = len(edges) for edge in range(array_length): node1, node2 = edges[0], edges[1] adj[node1][node2] += 1 adj[node2][node1] += 1 return adj first_multiple_input = input().rstrip().split() n = int(first_multiple_input[0]) k = int(first_multiple_input[1]) edges = [] for _ in range(n - 1): edges.append(list(map(int, input().rstrip().split()))) similarPair(n, k, edges) G = Graph(nodes, edges) copy and paste
@DavidAmos
@DavidAmos 3 жыл бұрын
​@@arnantaroy7468 Thanks for providing your code. So, there are two issues that I see. First, in the for loop in the adjacency_matrix() function you are looping over range(array_length) which is basically a list from 0 to array_length - 1. In my original adjacency_matrix() function, I loop over the edges in the graph, not a list of numbers. So the for loop should start like this: for edge in edges: Next, you are using edges[0] and edges[1] to get the two nodes in each edge. But edges is a list of lists, so edges[0] return [5, 6] and edges[1] returns [2, 3]. Instead, you should use: node1, node2 = edge[0], edge[1] # Note there is so s at the end of edge That will use the edge from the current step in the loop (such as the edge [5, 6] on the first step) and assign 5 to node1 and 6 to node2. That will get rid of the TypeError you saw. However, you'll then get an IndexError. That's because after you input your edges to your program, the list of your nodes is [5, 6, 2, 3]. But my adjacency_matrix() function assumes that the nodes list is [0, 1, 2, 3] for a graph with four nodes (It's in the docstring: "Assumes that graph.nodes is equivalent to range(len(graph.nodes))"). I'm not sure if your graph is supposed to have four nodes or not, or if the node labels you input to your program are weights for each node or what. The adjacency matrix function from my video won't work with weighted graphs as-is. It'll need to be modified, if that is what you are working with here. Anyway, hope that helps at least a little bit!
@sinnvollerkommentar263
@sinnvollerkommentar263 3 жыл бұрын
Cant wait for the next one. Guess I do a sleep() Typical mathematican. Keeping track of every click using bitly to analyse later. 😃
@DavidAmos
@DavidAmos 3 жыл бұрын
I haven't looked at the bitly stats for any of these links, haha! I just used it to shorten the links for the description.
@yogeshbhatt4702
@yogeshbhatt4702 2 жыл бұрын
Lord please give me strength so that I can bear this 36 minutes of mental pain
@ViraLok
@ViraLok 3 жыл бұрын
🇫🇷️👍️
Как Я Брата ОБМАНУЛ (смешное видео, прикол, юмор, поржать)
00:59
Can You Find Hulk's True Love? Real vs Fake Girlfriend Challenge | Roblox 3D
00:24
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49
HARD_MMA
Рет қаралды 3,3 МЛН
Players push long pins through a cardboard box attempting to pop the balloon!
00:31
G-3. Graph Representation in Java | Two Ways to Represent
13:13
take U forward
Рет қаралды 156 М.
10 Python Comprehensions You SHOULD Be Using
21:35
Tech With Tim
Рет қаралды 158 М.
GTAC 2.6: Implementing a Graph Data Structure in Python
31:17
Don Sheehy Lectures
Рет қаралды 22 М.
G-2. Graph Representation in C++ | Two Ways to Represent
16:04
take U forward
Рет қаралды 336 М.
Creating Your Own Programming Language - Computerphile
21:15
Computerphile
Рет қаралды 124 М.
Is This The Best Graph Theory Book Ever?
13:28
David Amos
Рет қаралды 16 М.
Degrees and Degree Sequences | Graph Theory With Python #4
39:30
David Amos
Рет қаралды 4,7 М.
Как Я Брата ОБМАНУЛ (смешное видео, прикол, юмор, поржать)
00:59