Introduction to Graphs in Python – Useful code


Lately, I am reading the book Optimization Algorithms by Alaa Khamis and the chapter 3 – Blind Search Algorithms, has caught my attention. The chapter starts with explaining what graphs are how these are displayed in python and I have decided to make a YT video, presenting the code of the book with Jupyter Notebook.

Trees are different, when we talk about graphs in python

Why graphs? Because they are everywhere:

  • A road map is a graph
  • Your social-media friends form a graph
  • Tasks in a to-do list, with dependables on each other, can be a graph

With Python we can build and draw these structures in just a few lines of code.

Setup

Undirected graph

  • Edges have no arrows
  • Use it for two‑way streets or mutual friendships.

Undirected graph

Directed graph

  • Arrowheads show direction.
  • Good for “A follows B” but not the other way around.

Directed graph

Multigraph

  • Allows two or more edges between the same nodes.
  • Think of two train lines that join the same pair of cities.

Multigraph

Directed Acyclic Graph (Tree)

  • No cycles = no way to loop back.
  • Used in task schedulers and Git histories.

Directed Acyclic Graph (Tree)

Hypergraph

  • One “edge” can touch many nodes.
  • We simulate it with a bipartite graph: red squares = hyper‑edges.

Hypergraph

Weighted Graph

  • Graph with weights on the edges
  • Idea for mapping distances between cities on a map

Weighted Graph

https://www.youtube.com/watch?v=8vnu_5QRC74

🙂



Source link

دیدگاه‌ها

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *