Python: 2 Ways to Represent Graphs


Video is ready, Click Here to View ×

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,
See subtitles en Francais.

16 thoughts on “Python: 2 Ways to Represent Graphs

  1. Hi Joe, nice of you. You are doing an awesome job for the community. I have one query regarding your Adjacency Matrix representation. Lat's say I create just two vertices 'A' & 'B' and create an edge between this two and print them. Your code would print: A 01 B 01. I think that doesn't look correct representation for me. A 01 B 10, isn't it. A 01 is correct because there is no edge between A & A and there is edge between A & B. B 0 1 should ideally be B 10 as there is an edge between B & A and there is NO edge between B & B. What do you think?

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

  3. Hello Joe. I got this error on Python 2.7:

    Traceback (most recent call last):

    File "<ipython-input-60-2d70617374f2>", line 36, in <module>
    a = Vertex('A')

    File "<ipython-input-60-2d70617374f2>", line 4, in _init_
    self.neighbors = list()

    TypeError: 'list' object is not callable

    Any idea about what is going on??

  4. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *