Python: 2 Ways to Represent GRAPHS
Вставка
- Опубліковано 15 лип 2024
- Two main ways of representing graph data structures are explained: using Adjacency Lists, and an Adjacency Matrix. This video also shows how to implement code for both in Python 3.
Download the latest code from my github site here, github.com/joeyajames/Python
See subtitles en Francais. - Наука та технологія
intro music fits well to my mood watching this right before my finals not knowing anything
lol
How did it go?
@@kebman Another year into my Computer Science degree. Looking back at this, it seems so easy! Thanks for your help!!
Stopping by to tell you I've learned something from this video. Thanks!
You are just awesome! Many thanks for your great tutorials.
Line 14 at 7:45 timestamp, the for loop is confusing. Could you please explain how for row in self.edges working? how can you append 0 to the row??
Thanks a lot for this video, it helps me a lot, how would you modify the code if you want to remove one or many vertices ?
Thank you sir...The video is very informative...
Excellent Joey.
i like the videos, very advanced, although Im still beginner but im already interested to learn this your vids :)
Thanks so much for this!
Thank you so much for this! I was utterly confused after my university lecture but this cleared up my doubts.
Which lecture is this, may I ask?
mr joe james, i am making a graph with 644 nodes, and I have a txt file with all the data information for each node:value. how would I be able to graph this data, as well as correctly importing the data from the file that I already have?
Hello Mr. James. Thank you for your tutorials. I learned a lot from them.
In your code for representing a graph with adjacent matrix with edge weigh, how would you modify the code if all the edges have different weights? Thank you in advance.
I don't think you would have to change anything, just when adding an edge you send 3 parameters (u, v, wt) instead of 2. The add_edge function has a default of weight=1 that gets used if you only send 2 params.
One more question what is the need of class Vertex in Adjacency Matrix code. Can we do it in one class?
Useful.... thank you
Must be trivial to _compress_ the Adjacency Matrix...?
Depending on the query, the compression could perhaps be dynamic as well.
Or what about a combination of the two, meaning a compressed matrix with a list containing more metadata.
Thanks for this wonderful video, a blessing indeed. Appreciate the efforts, what changes for the directed graph
you dont automatically add the v , u index for u,v index that's it
Which one is better graphs are implemented by using linked list or dictionaries??
Thank you :)
Thank you.
Thanks bro !
Sir, is there some python package for drawing graphs?
Hello Joe. I got this error on Python 2.7:
Traceback (most recent call last):
File "", line 36, in
a = Vertex('A')
File "", line 4, in __init__
self.neighbors = list()
TypeError: 'list' object is not callable
Any idea about what is going on??
+Andres Leiva sorry I'm on the road right now so can't bring up my code. I wrote all my Python videos in Python 3.x and there are a few things you have to change to get my code running in 2.7. The first things you shou change are format all print statements and input or raw input statements for 2.7. And it looks like you have to use different syntax for instantiating lists. Try self.neighbors = []
Why are you looping through vertices when you can simply do:
self.vertices['v.name'].add_neighbour(u)
self.vertices['u.name'].add_neighbour(v)
was thinking the about the same thing lol
I think it should be:
self.vertices[v].add_neighbor(u)
self.vertices[u].add_neighbor(v)
@@daveconrad8755 correct
Please can u explain how to use mxgraph with python
You're the man!
Thanks
@@oggiai Hey I have a question. How do I implement an algorithm to reduce the graph from weighted to an unweighted graph using the adjacency list way? Let’s say I’m given a set of edge weights like {1, 2} in a weighted undirected graph. How do I transform a graph so that all edge weights in a graph are equal (where preferably each edge weight in new graph equals to 1)? I know I may have to add or remove edges and intermediate vertices but is there a way to think about this?
why do we have to return true or false in add_vertex and add_edge in class graph
Suryansh Singh you don’t necessarily have to, but it’s good practice to send back some kind of confirmation so the caller knows what the result was. Some callers may not use your return value, but it’s there if they need it.
what if there is 1 in A A, B B, and so on?
Thank you for the video. Very helpful. A couple of follow up questions. Could you share functions for bfs or dfs or finding shortest path? Also, do you need to add a flag for 'visited' in the vertex when searching?
Use Dijkstra algorithm for shortest path
hi i was wondering if you could help me create a function for the distance matrix of an undirected graph? I'm not sure how to do it but I think the function nx.adjacency_matrix(G) can be used?
You should watch my videos on Dijkstras or other graph algorithms.
@@oggiai okay thank you!
Can you explain why we are using Ord for the test code?
the Range function I used for the for loop requires integer arguments. Ord converts a character to its ASCII integer equivalent so the for loop can iterate it. Then when a letter is added inside the for loop we use chr(i) to convert it back into a character.
I had the intro down to 5% and it was still perfectly audible.
Hey Joe. How can we implement this in unweighted graph data structure
+Asdee gen you could just use edge weight of 1 for all edges
Thanks for your reply!.
Great video. What do you mean by neighbour?
Any node that is one hop away, meaning there is an edge between the two nodes.
Any node that is one hop away, meaning there is an edge between the two nodes.
Ok thank youu😀
3:06 About equal to or equal to? your notation suggests equal to
Density is non-binary, so about equal to.
why didn't you use a set instead of list for neighbors?
You sure could use a set, but the set is unordered
Wtf is that intro
I was full volume with my earphones ahahah
I think we don't need self.vertices while we can represent the same by self.edge_indices.keys()
but you could have a vertex with no edges.
Yeah! great.... thank you
Thank you ...but. I want to Ask you about how to create a graphe and to move from A to B u must to Say the way to used it to arrived to B ( example : A==> B we use caractère a...z , than from A to C we use a number 0....9 ,than from A to D we use a comment ( comment contain the("") ..
It sounds like you just need a graph traversal function that receives a few additional optional parameters, then checks using if statements which parameter is received.
def get_next_node (node, char=None, num=None, comment=None):
if char return nodeB
elif num return nodeD
elif comment return nodeE
Sorry hacked together and typed on my phone.
When you defined "edge_indices" I think you meant "vertex_indices"
So how to traverse the Graph?? it is not presentation only, you know we have to traverse and find shortest path etc..
I have separate videos on various graph traversal. You should browse through my other Python videos
@@oggiai I will, Thank you very much
What a starting music,
I took that out from my newer videos
Ok, so the difference between the two is that one is a list, and the other is an Excel spreadsheet. Got it!11
LOL. Yeah, one central 2D list vs a 1D list in each node.
can you translate this video on french please??
+asmaa arsa oui, biensur. Mais ca faudra quelques semaines.
merci beaucoup, parce que j'ai un projet fin etude en coloration packing graphe en langage python et dernièrement j'ai appris langage python,j e ne suis pas tres fort pour coder en python
J'ai finis les subtitres pour la plupart du video. J'espere que c'est assez bien fait.
English should be Ok when you are programming as most documentation is written in it or will be communicated in. You can't expect that others pick up the language barrier when it is you. It's a different thing when you translate it to your friends, but as programmer English should be a must. In Germany even for A-levels, IT stuff is mostly bilingual and should be so in French-speaking contries at the least.
Why is your intro so scary?
I dropped the music on all my newer videos
Wtf with that intro
y it has such a horrible intro!!