Introduction to Graph Data Structures and Algorithms

General Concepts and Definitions of Graphs

  • Definition of a Graph: A graph is an abstraction used to represent objects and the multiple relationships between them. These notes focus on graphs within the context of Data Structures and Algorithms at the Universidad de los Andes, Colombia.
  • Formal Definition: A graph is denoted as G=⟨V,E⟩G = \langle V, E \rangle, where:
    • VV is a set of vertices or nodes (the objects of interest).
    • EE is a set of arcs or edges (the relationships between pairs of objects).
  • Why Study Graphs? Concepts from graphs are ubiquitous. Search tools like Google analyze graph data to determine the relevancy of information.
  • Applications Across Disciplines:
    • Computer Networks: Modeling connectivity and data flow.
    • Ecology: Analyzing relationships between organisms in ecosystems (e.g., food webs involving items like Clam, Widgeon Grass, Blue Crab).
    • Artificial Intelligence (AI): Using knowledge graphs to represent information.
    • Neuroscience: Mapping connections between different regions of the brain (e.g., studies on the macaque brain).
    • Epidemiology: Modeling the spread of diseases, particularly in rural populations where connections radiate from a central resident.
    • Knowledge Graphs: Google rates the relevancy of webpages based on search terms and the number of relevant pages linking to them (referenced pages are considered adjacent nodes).

Historical and Theoretical Problems

  • The Seven Bridges of Königsberg: Located in Prussia (now Kaliningrad), the city had an island named Kneiphof (A) surrounded by two branches of the Pregel River. There were seven bridges (a,b,c,d,e,f,ga, b, c, d, e, f, g) crossing these branches. The problem was to determine if a person could walk through the city crossing every bridge exactly once.
  • Eulerian Path: This is a path that visits every arc (edge) exactly once. This concept stems from Leonard Euler’s solution to the Königsberg problem.
  • Game Modeling:
    • Optimal Moves: Graphs can determine the next optimal movement in games like Tic-Tac-Toe.
    • State Space: In these models, a node represents a state of the board, and an arc represents a possible transition between states.
  • The Boat, Cabbage, Goat, and Wolf Problem: A classic logic puzzle where a boat can carry only two entities at a time, but the goat cannot be left with the cabbage, and the wolf cannot be left with the goat. This sequence of states and transitions can be modeled as a graph.

Graph Classifications and Properties

  • Undirected Graph: A graph G=⟨V,E⟩G = \langle V, E \rangle where arcs represent a bilateral relationship between vertices. If e∈Ee \in E and u,v∈Vu, v \in V, then e(u,v)e(u, v) implies e(v,u)e(v, u).
  • Directed Graph: A graph where relations are unidirectional. Given u,v∈Vu, v \in V, the existence of e(u,v)∈Ee(u, v) \in E does not imply e(v,u)∈Ee(v, u) \in E. Flow only occurs in the direction of the arcs.
  • Social Graphs: Used to analyze relationships between people. These are typically undirected graphs and often exhibit the "small world" property.
  • Subgraph: A subset of nodes and arcs from the original graph.
  • Regular Graph: A graph is regular if all its nodes have the same degree. For instance, if every node has a degree of 2, then ∀i,deg(vi)=2\forall i, deg(v_i) = 2 and deg(G)=2deg(G) = 2.
  • Degree of a Vertex (deg(v)deg(v)): The number of arcs incident to a vertex. In co-authorship graphs, the size of a node often indicates the number of collaborations (degree) an author has.
  • Graph Order: The number of vertices in the graph, denoted as ∣V∣|V|.
  • Graph Size: The number of arcs/edges in the graph, denoted as ∣E∣|E|.
  • Complete Graph: A graph where every pair of distinct vertices is connected by a unique edge. A complete graph with nn vertices has exactly n×(n−1)2\frac{n \times (n-1)}{2} arcs.
  • Bipartite Graph: A graph where the vertices can be divided into two disjoint sets, AA and BB, such that every edge connects a vertex in AA to a vertex in BB. Formally: ∀e∈E,if e(u,v) then (u∈A∧v∈B)\forall e \in E, \text{if } e(u, v) \text{ then } (u \in A \wedge v \in B).
  • Planar Graph: A graph that can be drawn in a plane without any arcs crossing each other.

Paths, Cycles, and Connectivity

  • Walk (Camino): A sequence of vertices v1,v2,…,vn+1v_1, v_2, \dots, v_{n+1} and arcs e1,e2,…,ene_1, e_2, \dots, e_n such that ei=(vi,vi+1)e_i = (v_i, v_{i+1}) for 1≤i≤n1 \le i \le n. For example, a walk between v1v_1 and v7v_7 could be walk(v1,v7)=v1,v2,v5,v3,v2,v5,v6,v7walk(v_1, v_7) = v_1, v_2, v_5, v_3, v_2, v_5, v_6, v_7.
  • Trail (Recorrido): A walk that does not repeat any arcs. For example, trail(v1,v7)=v1,v2,v5,v3,v4,v5,v6,v7trail(v_1, v_7) = v_1, v_2, v_5, v_3, v_4, v_5, v_6, v_7.
  • Simple Path (Camino Simple): A trail where both vertices and arcs are distinct (no repetitions). For example, path(v1,v7)=v1,v2,v5,v6,v7path(v_1, v_7) = v_1, v_2, v_5, v_6, v_7.
  • Cycle (Ciclo): A simple path that starts and ends at the same vertex. A sequence where v1=vn+1v_1 = v_{n+1}. Example: cycle(v1)=v1,v2,v5,v3,v1cycle(v_1) = v_1, v_2, v_5, v_3, v_1.
  • Eulerian Path/Cycle: A path or cycle that passes through every arc exactly once.
  • Hamiltonian Path/Cycle: A path or cycle that passes through every vertex exactly once.
  • Connectivity in Undirected Graphs:
    • Connected: A graph is connected if a path exists between any pair of vertices.
    • Disconnected: A graph is disconnected if there exists at least one pair of vertices u,v∈Vu, v \in V such that no path exists between them.
  • Connectivity in Directed Graphs:
    • Strongly Connected: A directed graph is strongly connected if there is a directed path between every pair of vertices in both directions: ∀u,v∈V,∃path(u,v)∧path(v,u)\forall u, v \in V, \exists path(u, v) \wedge path(v, u).

Trees and Forests

  • Tree: A connected acyclic graph (contains no cycles).
  • DAG (Directed Acyclic Graph): A directed graph that contains no cycles.
  • Forest: A graph defined as a set of disjoint trees.
  • Spanning Tree Protocol: Used in network topologies to manage loops/cycles caused by redundant links, ensuring connection availability without broadcast storms.
  • Graph Isomorphism (Equivalent Graphs): Two graphs are equivalent if there exists a morphism (function ff) mapping their nodes such that the degree and adjacency of each node are preserved. For example, different drawings of the complete graph K8K_8 can be isomorphic.

Graph Representation Methods

Adjacency Matrix
  • Description: Models each vertex as both a row and a column. The position [i,j][i, j] indicates the capacity, cost, or weight cc of the arc between vertex ii and vertex jj. If no arc exists, the value is 0.
  • Advantages:
    • A row immediately shows the adjacent vertices of a node.
    • A column immediately shows the predecessors of a node.
  • Disadvantages:
    • Memory inefficiency: If the graph is sparse (low connectivity), many cells remain empty (0).
    • Redundancy: In symmetric matrices (undirected graphs), weights and connections are duplicated.
  • Python Implementation (Empty Graph):python def new_graph(n): """ Creates a new adjacency matrix (graph) with n vertices Params: n - Quantity of vertices in the graph Returns: New empty graph """ return {'matrix': [[0 for i in range(n)] for j in range(n)], 'num_edges': 0}     
Adjacency List
  • Description: Models each vertex with a list. For vertex ii, every entry in its list represents an arc to a vertex jj and includes its weight/cost cc.
  • Advantages:
    • Space efficiency for sparse graphs.
    • Direct access for a vertex to its adjacents.
  • Disadvantages:
    • Information may still be duplicated for undirected graphs.
  • Python Implementation (Empty Graph):python def new_graph(n): """ Creates a new adjacency list (graph) with n vertices Params: n - Quantity of vertices in the graph Returns: New empty graph (without vertices or arcs) """ return {'vertices': map.new_map(n, 0.5), 'num_edges': 0}     

Computational Problems on Graphs

  • Reachability: Determining if a path exists from a source vertex ss to a target vertex tt.
  • Shortest Path: Finding the "shortest" path between ss and tt based on:
    1. The path with the minimum number of arcs.
    2. The path with the lowest total edge cost.
  • Cycle Detection: Checking if the graph contains at least one cycle.
  • Route Planning: Intersections are treated as vertices, and street segments are arcs with associated distances (costs).

Abstract Data Type (ADT) Graph API

Primary Operations
  • new_graph(n): Creates an empty graph of order nn.
  • insert_vertex(graph, key_u, info_u): Adds a vertex with a unique key and information; initially has no adjacents. If it exists, information is replaced.
  • update_vertex(graph, key_u, info_u): Updates information for an existing vertex.
  • remove_vertex(graph, key_u): Removes the vertex and all incoming/outgoing arcs.
  • order(graph): Returns ∣V∣|V|.
  • size(graph): Returns ∣E∣|E|.
  • vertices(graph): Returns a list of vertex keys.
  • degree(graph, key_u): Returns the number of outgoing arcs from uu.
  • get_edge(graph, key_u, key_v): Returns the arc between uu and vv.
  • add_edge(graph, key_u, key_v, c): Adds or updates an arc from uu to vv with cost cc. For undirected graphs, this must be called twice: add_edge(u, v, c) and add_edge(v, u, c).
  • adjacents(graph, key_u): Returns a list of keys for vertices adjacent to uu.
  • get_vertex(graph, key_u): Returns all information associated with a vertex.
  • contains_vertex(graph, key_u): Returns True if the vertex exists, False otherwise.
Internal Data Structures
  • Vertex Definition:python def new_vertex(key, value): return {'key': key, 'value': value, 'adjacents': map.new_map(3, 0.5)}     
  • Edge Definition:python def new_edge(key_v, weight=0.0): return {'to': key_v, 'weight': weight}     

Resources

  • Textbook: Data Structures and Algorithms in Python by Michael T. Goodrich and Roberto Tamassia, Chapter 14, Sections 14.1 and 14.2.