liveBook · Manning

Graph

As explained in Grokking AI Algorithms, Second Edition

A graph is a data structure consisting of entities represented as vertices and relationships represented as edges.

A graph is a data structure made of states and connections. Each state is a node, or vertex; each connection is an edge. Graphs model relationships among objects and make many problems easy to visualize and process with algorithms. section 2.5

A road trip as a graph

Imagine traveling to the beach. Each stop becomes a node, each road becomes an edge, and the distance traveled becomes an edge weight. A search algorithm can then find a path through the connected stops. Similar graphs represent computer networks, web pages and links, or people and social relationships. section 2.5 section 2.13

Choose the graph category that matches the relationships and structure:

  • Undirected graphs have mutual relationships, like roads usable in both directions; directed graphs have explicitly one-way relationships. section 2.14
  • Disconnected graphs contain isolated groups or nodes; acyclic graphs contain no cycles, as with course prerequisites. section 2.14
  • Complete graphs connect every node to every other node; complete bipartite graphs connect every node in one partition to every node in another. section 2.14
  • Weighted graphs attach values such as distances to edges, helping algorithms distinguish between connections. section 2.14
Figure 2.26 Types of graphs

Use the graph-category figure to compare direction, cycles, connectivity, partitions and edge weights visually. section 2.14

For implementation, represent relationships with an array of arrays, an incidence matrix, an adjacency matrix or an adjacency list. An adjacency records a node directly connected to another node; the best encoding depends on processing needs, language and tools. When searching graphs that contain cycles, track visited nodes to avoid looping forever. section 2.6 section 2.13

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Defines nodes, edges, weights, categories and several representations, but stops short of implementation code or algorithmic analysis.
Examples
Uses road trips, computer networks, web crawlers, social relationships, course prerequisites, teams, cheese tastings and city distances.
What it assumes
Assumes basic data-structure familiarity while defining graph terminology and representations from the beginning.
What you can do after
Build it from scratch by choosing a graph category and representing its nodes and relationships for algorithmic processing.
How it connects
Connects graph theory, search algorithms, depth-first search, adjacency matrices, adjacency lists and visited-node tracking.

Also explained in

Compare how 21 books explain graph →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.