Interactive deep-dive on Graphs: what nodes and edges are, types of edges, types of graphs, and a live graph builder.

Back to DSA Python report

Graphs Deep Dive

Interactive notes for graph vocabulary, edge types, graph types, tree intuition, representations, and a live graph builder.

Graphs — deep dive interactive

What are nodes and edges?

Node (vertex)

A thing — a data point, a place, a person, an entity.

Drawn as a circle. Holds a value or label.

Edge

A connection between two nodes — drawn as a line between circles.

Can carry direction, weight, or just be a plain link.

Click on any circle or line below to learn more:

Social network Alicenode Bobnode Carolnode Davenode friends friends friends Map / roads 5 km 8 km 3 km 2 km A B C D Click any node above

Types of edges

Edges carry different properties depending on the problem you're modelling.

Undirected edge A B A—B: both can reach each other Example: Facebook friendship Directed edge (arc) A B A→B: only A can reach B this way Example: Twitter follow Weighted edge X 42 Y Edge has a cost / distance / time Example: road distance in km Self-loop & multi-edge P self-loop Q multi-edge Node pointing to itself, or two nodes with multiple connections Example: parallel flights

Edge in Python (adjacency list)

undirected: graph['A'].append('B')
            graph['B'].append('A')

directed:   graph['A'].append('B')
            # B does NOT point back to A

weighted:   graph['A'].append(('B', 42))
            # tuple: (neighbor, weight)

Real-world edge analogies

Edge typeReal example
UndirectedWhatsApp contact
DirectedInstagram follow
WeightedFlight price
Self-loopA page linking to itself

Types of graphs

Undirected A B C Edges are two-way Facebook, roads Directed (Digraph) A B C Edges have direction Twitter, one-way streets Weighted A B C 5 3 9 Edges have cost/weight Maps, flight costs
1 / 3

BST — Binary Search Tree

Full form breakdown

Binary — each node has at most 2 children (left & right)

Search — organized for fast searching: left < node < right

Tree — hierarchical, acyclic structure with one root

The golden rule

left < node < right

For every node: everything in its left subtree is smaller, everything in its right subtree is larger.

Search for value: —

8 3 12 1 6 10 14 Select a value to search:
Search for:

Why BST is powerful — O(log n) search

Each comparison eliminates half the tree. With 1,000,000 nodes: only ~20 comparisons needed. That's the power of O(log n) vs O(n) linear search.

def search(node, target):
    if not node or node.val == target:
        return node          # found or not here
    if target < node.val:
        return search(node.left, target)   # go left
    return search(node.right, target)      # go right

Build your own graph

Click on the canvas to add a node.

Adjacency list (live)

{}

How graphs are stored in Python

1. Adjacency list — most common

from collections import defaultdict

graph = defaultdict(list)
graph['A'].append('B')
graph['A'].append('C')
graph['B'].append('D')
graph['C'].append('D')

# Result:
# {'A': ['B', 'C'], 'B': ['D'], 'C': ['D'], 'D': []}

Space: O(V + E). Best for sparse graphs.

2. Weighted adjacency list

graph = defaultdict(list)
graph['A'].append(('B', 5))   # (neighbor, weight)
graph['A'].append(('C', 3))
graph['B'].append(('D', 8))

# Iterate over weighted neighbors:
for neighbor, weight in graph['A']:
    print(neighbor, weight)    # B 5, then C 3

3. Adjacency matrix

# 4 nodes: A=0, B=1, C=2, D=3
matrix = [
    [0, 1, 1, 0],  # A connects to B and C
    [0, 0, 0, 1],  # B connects to D
    [0, 0, 0, 1],  # C connects to D
    [0, 0, 0, 0],  # D has no outgoing edges
]

# Check if edge A→B exists:
if matrix[0][1] == 1: print("A→B exists")

Space: O(V²). O(1) edge lookup. Best for dense graphs.

PropertyAdjacency listAdjacency matrix
SpaceO(V + E)O(V²)
Edge checkO(degree)O(1)
Add edgeO(1)O(1)
Get all neighborsO(degree)O(V)
Best forSparse, most real graphsDense, small graphs