GRAPHS
Trees Without Rules
Graph
A data structure that consists of a set of nodes
(called vertices) and a set of edges that relate the
nodes to each other
Facebook Graph
Vertices: people
Edges: friends
June
Katie
Susan
Sarah
Judy
Lila
Robert
Rebecca
John
Airline Routes Graph
Course Prerequisites Graph
Graphs
Another recursive data structure
Node with any number of pointers to other nodes
No restrictions on connectivity
Allows disconnected nodes, multiple paths, and cycles
Terminology
Node
Arc
Directed
Connected
Path
Cycle
Implementation Strategies
Create a set of all arcs
(a->b, a->c, b->d, c->d, d->a, d->b)
a
Adjacency List
a: {b, c}
b: {d}
c:{d}
d:{a, b}
Adjacency matrix
Consider the data
Sparse vs. Dense
Space vs. Time
c
b
d
Coding Graphs
Graph itself is set or vector of node *
Why
not just pointer to a root, like a tree?
Could you designate an arbitrary node as root?
Struct nodeT {
//data for node goes here
vector <nodeT *> connected;
};
Coding Graphs
Often graphs have data associated with the arc
itself.
Unlike
trees and lists where links are only for connecting
nodes
Arcs may have information like distance or cost.
Add
struct to hold arc info
Arc
has pointers to start/end node and a node has a
collection of arcs
Circular
reference
Circular Reference
arcT
nodeT
Arcs have Nodes and Nodes have Arcs
struct arcT {
//arc fields go here
nodeT *start, *end;
//error! nodeT not defined
};
struct nodeT {
//node fields go here
vector <arcT *> outgoing; //arcT already defined
};
Making the Gears Work
struct nodeT; //forward reference
struct arcT {
//arc fields go here
nodeT *start, *end;
};
struct nodeT {
//node fields go here
vector <arcT *> outgoing;
};
Graph Traversals
Traverse reachable nodes
Start from a node and follow arcs to other nodes
Some graphs not fully connected, so not all nodes are
reachable
Depth-first and Breadth-first
Similar to tree’s post-order, pre-order and in-order
Both visit all reachable nodes, but in a different order
Possibility of cycles means you must track “visited” nodes to
avoid infinite loop
Could maintain a visited flag per node or set of visited nodes
Depth-First Traversal
Choose a starting node
Go Deep
Pick a neighbor, explore all reachable from there
Backtrack
No root node in graph, may have specific start in mind or
just choose one randomly
After fully exploring everything reachable from first
neighbor, choose another neighbor and go again
Continue until all neighbors are exhausted
Base case?
Depth-First Pseudocode
void DepthFirstSeach(node *cur, set<nodeT *> & visited)
{
//if visited contains cur, do nothing.
//otherwise, add cur to visited set
//for all arcT’s in the graph vector named outgoing
DepthFirstSearch(cur->outgoing[i]->end, visited);
}
Trace
A
H
B
D
G
C
E
ABCDEFGH
F
Breadth-First Traversal
Choose starting node
Visit all immediate neighbors
Branch out to all neighbors 2 hops away
Again to 3 hops and so on
Until all reachable nodes are visited
How to manage nodes to vist
Those directly connected to start node
Perfect for the queue
What about cycles/multiple paths?
Need to track visited status
Breadth-First Pseudocode
void BreadthFirstSearch(nodeT *start)
{
queue<nodeT *> q;
set<nodeT *> visited;
q.enqueue(start);
//while q is not empty
nodeT *cur = q.dequeue();
//if visited does not contain cur, add cur to visited and
//for all arcTs in vector named outgoing...
q.enqueue(cur->outgoing[i]->end);
}
Graph Search Algorithms
Many interesting questions are really just graph
searches
Which
nodes are reachable from this node?
Does the graph have a cycle?
Is the graph fully connected?
Longest path without a cycle?
Is there a continuous path that visits all nodes once and
exactly once?
© Copyright 2026 Paperzz