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.
  • Наука та технологія

КОМЕНТАРІ • 82

  • @pkgamma
    @pkgamma 5 років тому +92

    intro music fits well to my mood watching this right before my finals not knowing anything

    • @MrBARA1000
      @MrBARA1000 4 роки тому

      lol

    • @kebman
      @kebman 3 роки тому +1

      How did it go?

    • @pkgamma
      @pkgamma 3 роки тому +4

      @@kebman Another year into my Computer Science degree. Looking back at this, it seems so easy! Thanks for your help!!

  • @danielalmeida7140
    @danielalmeida7140 7 років тому +1

    Stopping by to tell you I've learned something from this video. Thanks!

  • @vincentkizza3751
    @vincentkizza3751 3 роки тому

    You are just awesome! Many thanks for your great tutorials.

  • @AnilYadav-ut5zj
    @AnilYadav-ut5zj 6 років тому +2

    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??

  • @asmaacherief6558
    @asmaacherief6558 4 роки тому

    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 ?

  • @anjalipc8649
    @anjalipc8649 3 роки тому +1

    Thank you sir...The video is very informative...

  • @kaleabwoldemariam4288
    @kaleabwoldemariam4288 3 роки тому

    Excellent Joey.

  • @charlescham2024
    @charlescham2024 8 років тому

    i like the videos, very advanced, although Im still beginner but im already interested to learn this your vids :)

  • @Juceeass
    @Juceeass 4 роки тому

    Thanks so much for this!

  • @toviyahu
    @toviyahu 4 роки тому +2

    Thank you so much for this! I was utterly confused after my university lecture but this cleared up my doubts.

    • @chloem.872
      @chloem.872 4 роки тому

      Which lecture is this, may I ask?

  • @rohankosalge6025
    @rohankosalge6025 5 років тому

    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?

  • @oliveradimoska9796
    @oliveradimoska9796 8 років тому +5

    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.

    • @oggiai
      @oggiai  8 років тому +3

      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.

  • @asdeegen3480
    @asdeegen3480 7 років тому

    One more question what is the need of class Vertex in Adjacency Matrix code. Can we do it in one class?

  • @everboy112
    @everboy112 4 роки тому +1

    Useful.... thank you

  • @kebman
    @kebman 3 роки тому

    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.

  • @mugumyakevin8696
    @mugumyakevin8696 4 роки тому +3

    Thanks for this wonderful video, a blessing indeed. Appreciate the efforts, what changes for the directed graph

    • @diptadutta6205
      @diptadutta6205 3 роки тому

      you dont automatically add the v , u index for u,v index that's it

  • @malluvalasajohnkumar6238
    @malluvalasajohnkumar6238 4 роки тому

    Which one is better graphs are implemented by using linked list or dictionaries??

  • @irshadalam5345
    @irshadalam5345 8 років тому +1

    Thank you :)

  • @codyfivecoat7621
    @codyfivecoat7621 2 роки тому +1

    Thank you.

  • @MrBARA1000
    @MrBARA1000 4 роки тому +1

    Thanks bro !

  • @DrIlyas-sq7pz
    @DrIlyas-sq7pz 3 роки тому

    Sir, is there some python package for drawing graphs?

  • @Andru358
    @Andru358 7 років тому +2

    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??

    • @oggiai
      @oggiai  7 років тому

      +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 = []

  • @mananmehta
    @mananmehta 8 років тому +12

    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)

    • @halcyonramirez6469
      @halcyonramirez6469 6 років тому +1

      was thinking the about the same thing lol

    • @daveconrad8755
      @daveconrad8755 4 роки тому +5

      I think it should be:
      self.vertices[v].add_neighbor(u)
      self.vertices[u].add_neighbor(v)

    • @eskoo8396
      @eskoo8396 3 роки тому +1

      @@daveconrad8755 correct

  • @sowmyashetty533
    @sowmyashetty533 4 роки тому

    Please can u explain how to use mxgraph with python

  • @bostevens236
    @bostevens236 4 роки тому +1

    You're the man!

    • @oggiai
      @oggiai  4 роки тому

      Thanks

    • @dewman7477
      @dewman7477 3 роки тому

      @@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?

  • @sensei249
    @sensei249 4 роки тому +2

    why do we have to return true or false in add_vertex and add_edge in class graph

    • @oggiai
      @oggiai  4 роки тому +2

      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.

  • @ethanblackthorn3533
    @ethanblackthorn3533 3 роки тому

    what if there is 1 in A A, B B, and so on?

  • @safwans1
    @safwans1 6 років тому

    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?

    • @hughe29
      @hughe29 Рік тому

      Use Dijkstra algorithm for shortest path

  • @uuddin
    @uuddin 5 років тому

    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?

    • @oggiai
      @oggiai  5 років тому

      You should watch my videos on Dijkstras or other graph algorithms.

    • @uuddin
      @uuddin 5 років тому

      @@oggiai okay thank you!

  • @JoshT4life
    @JoshT4life 4 роки тому

    Can you explain why we are using Ord for the test code?

    • @oggiai
      @oggiai  4 роки тому

      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.

  • @ShermanSitter
    @ShermanSitter 4 роки тому +3

    I had the intro down to 5% and it was still perfectly audible.

  • @asdeegen3480
    @asdeegen3480 7 років тому

    Hey Joe. How can we implement this in unweighted graph data structure

    • @oggiai
      @oggiai  7 років тому +1

      +Asdee gen you could just use edge weight of 1 for all edges

    • @asdeegen3480
      @asdeegen3480 7 років тому

      Thanks for your reply!.

  • @nicolasquintanaariste6614
    @nicolasquintanaariste6614 4 роки тому

    Great video. What do you mean by neighbour?

    • @oggiai
      @oggiai  4 роки тому

      Any node that is one hop away, meaning there is an edge between the two nodes.

    • @oggiai
      @oggiai  4 роки тому

      Any node that is one hop away, meaning there is an edge between the two nodes.

    • @nicolasquintanaariste6614
      @nicolasquintanaariste6614 4 роки тому

      Ok thank youu😀

  • @pedrosso0
    @pedrosso0 2 роки тому

    3:06 About equal to or equal to? your notation suggests equal to

    • @oggiai
      @oggiai  2 роки тому +1

      Density is non-binary, so about equal to.

  • @KashMan523
    @KashMan523 4 роки тому

    why didn't you use a set instead of list for neighbors?

    • @oggiai
      @oggiai  4 роки тому

      You sure could use a set, but the set is unordered

  • @im_the_operator_with_my_po1684
    @im_the_operator_with_my_po1684 2 роки тому

    Wtf is that intro
    I was full volume with my earphones ahahah

  • @balajib6677
    @balajib6677 6 років тому

    I think we don't need self.vertices while we can represent the same by self.edge_indices.keys()

    • @oggiai
      @oggiai  6 років тому +1

      but you could have a vertex with no edges.

    • @balajib6677
      @balajib6677 6 років тому

      Yeah! great.... thank you

  • @hassanouddane263
    @hassanouddane263 4 роки тому

    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("") ..

    • @oggiai
      @oggiai  4 роки тому

      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.

  • @eskoo8396
    @eskoo8396 3 роки тому

    When you defined "edge_indices" I think you meant "vertex_indices"

  • @IMateme
    @IMateme 4 роки тому

    So how to traverse the Graph?? it is not presentation only, you know we have to traverse and find shortest path etc..

    • @oggiai
      @oggiai  4 роки тому +1

      I have separate videos on various graph traversal. You should browse through my other Python videos

    • @IMateme
      @IMateme 4 роки тому

      @@oggiai I will, Thank you very much

  • @yashyadav-iz5un
    @yashyadav-iz5un 4 роки тому

    What a starting music,

    • @oggiai
      @oggiai  4 роки тому

      I took that out from my newer videos

  • @kebman
    @kebman 3 роки тому

    Ok, so the difference between the two is that one is a list, and the other is an Excel spreadsheet. Got it!11

    • @oggiai
      @oggiai  3 роки тому +1

      LOL. Yeah, one central 2D list vs a 1D list in each node.

  • @asmaaarsa2957
    @asmaaarsa2957 7 років тому

    can you translate this video on french please??

    • @oggiai
      @oggiai  7 років тому

      +asmaa arsa oui, biensur. Mais ca faudra quelques semaines.

    • @asmaaarsa2957
      @asmaaarsa2957 7 років тому

      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

    • @oggiai
      @oggiai  7 років тому

      J'ai finis les subtitres pour la plupart du video. J'espere que c'est assez bien fait.

    • @h.h.c466
      @h.h.c466 6 років тому +1

      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.

  • @uruta09
    @uruta09 Рік тому

    Why is your intro so scary?

    • @oggiai
      @oggiai  Рік тому

      I dropped the music on all my newer videos

  • @bmanicharanreddy7386
    @bmanicharanreddy7386 4 роки тому

    Wtf with that intro

  • @shubhamrangadal
    @shubhamrangadal 6 років тому +1

    y it has such a horrible intro!!