Sanders: Graphs 1
Graphs and Graph Representation INFORMATIK
¾ 1736 L. Euler asks about a
¾ Street or Computer networks
¾ Railway connections (space and time)
¾ Citation or Coauthorship social networks
¾ Job precedences scheduling problems
¾ Values and arithmetic operations compiler construction
1 Mathematical View: Just Sets and Pairs INFORMATIK
Often terminology is already reason enough for a graph model
1.1 Directed Graphs
´Î µ describes a directed graph with vertex set Î and edge set
Convention: and Ñ
1.1.1 Special Cases (usually disallowed)
Multigraphs: parallel edges allowed, i.e, is a multiset.
self loops: edges ´Ú Úµ
´Ù ´Ú INFORMATIK
Bidirected graph: Úµ Ùµ
1.2 Undirected Graphs
streamlined description of a bidirected graph where edges are two element
° ´Ù ´Ú
ÙÚ Úµ Ùµ
degree´Úµ in-degree´Úµ out-degree´Úµ
What about self loops?
Sanders: Graphs 5
1.3 Subgraphs INFORMATIK
¼ ´Î ¼ ¼ µ is subgraph of µ if Î ¼ ¼¾
¼ is vertex induced by Î ¼ if ¼ ¼ ¼
¾ ¾ ¾
´Ù Úµ Ù Î Ú Î
1.4 Associated Information INFORMATIK
edge weights Û
Example: Length or trafﬁc capacity of a street,
we will see more things like node potentials, node/vertex colors,
Sanders: Graphs 7
1.5 Paths INFORMATIK
A path Ô Ú¼ Ú
of length connects nodes Ú¼ and Ú if
subsequent nodes in Ô are connected by edges in , i.e,
¾ ¾ ¾
´Ú¼ Ú½ µ ´Ú½ Ú¾ µ ´Ú µ
,¾ , , .
edge representation: .
simple path: Ú¼ , ,Ú different.
Nonsimple paths are usually called walks.
In weighted graphs, length is ½ Û´
bottleneck weight: Ñ Ò µ.
Applications: What is the shortest route from Saarbrucken to Paris?
How much trafﬁc can it carry?
1.6 Cycles INFORMATIK
Cycles are paths that share the ﬁrst and the last node
A cycle is simple if all other nodes are different.
Hamiltonian cycle: simple cycle that visits all nodes.
Euler cycle: cycle (cyclic walk) that visits all edges once
back to the bridge problem. But we need one more term.
Sanders: Graphs 9
1.7 Connectedness INFORMATIK
is strongly connected path Ô connects Ù and Ú
ÙÚ Î Ô
drop the “strongly” for undirected graphs
A connected component is a maximal connected vertex induced subgraph
1.8 Euler Tours INFORMATIK
Theorem 1. is Eulerian (has an Euler tour) if and only if is connected
and every node has even degree.
Proof: Necessity: connectedness is clear.
Consider a node Ú with degree ¾
The tour is stuck when it visits Ú the -th time.
Step 1, Construct Euler Paritioning of into a set of cycles:
Greedily build any walk. Remove visited edges.
When you get stuck, you have closed a cycle.
Step 2, Glue the cycles together.
Possible since any cycle intersects some other cycle.
Sanders: Graphs 11
1.9 Trees INFORMATIK
An undirected graph is a tree if there is exactly one path between any pair of
Exercise: Prove that the following properties are equivalent
1. is a tree.
2. is connected and has exactly Ò
3. is connected and contains no cycles.
Forest: undirected acyclic graph
Directed acyclic graph (DAG): no cycles.
Exercise: How many edges are possible?
1.10 Directed Trees? INFORMATIK
we need a special root node Ö.
exactly one Ö path or
Ú Î Ú
exactly one Ú path
Ú Î Ö
CS trees have the root at the top.
a parent is immediately above its children or successors which are siblings.
(ancestors, ) Nodes without children are leaves.
Nonroot nonleaf nodes are interior.
ordered trees: order of children matters. Example: search trees.
Sanders: Graphs 13
2 Graph Representation INFORMATIK
we mostly represent undirected graphs as bidirected graphs
focus on directed graphs
¾ Operations are what matters
¾ A trivial representation
¾ Linked Lists
¾ Implicit Representation
2.1 Operations INFORMATIK
Ç´½µ time for all elementary operations
Access associated information. Use arrays. Perhaps hash tables.
One reason for Î Ò.
Navigation: Given Ú ﬁnd outgoing edges. (We may also want incoming edges.)
Edge queries: ´Ù ? Adjanceny matrices, hash tables.
Reverse access: Given ´Ù ﬁnd ´Ú
Construction conversion and output (Ç´Ñ · Òµ time)
Update: Insert/delete nodes/edges. This is the hard part.
Sanders: Graphs 15
Example: Recognizing DAGs INFORMATIK
¾ ¼ do delete Ú
if Î then output “ is a DAG”
else output “ contains a cycle”
// to ﬁnd a cycle, pick any node and follow edges
works because we never destroy or introduce cycles
Exercise: implement it in time Òµ
2.2 Edge Sequence Representation INFORMATIK
Sequence of node pairs (or triples with edge weight)
· Good for I/O
Almost no useful operations except scanning all edges
Not so bad with some additional node arrays.
Examples: Find isolated nodes, several MST algorithms, conversion.
Sanders: Graphs 17
2.3 Adjacency Arrays INFORMATIK
Edge array E stores targets
grouping edges leaving a node
Node array V stores index of ﬁrst outgoing edge
stores Ñ · ½
dummy entry Î Ò
0 n−1 4=n
V0 2 466
E1 2 2 3 1 3
Example: out-degree´Úµ Ú
2.4 Edge List Adjacency Array INFORMATIK
A Reminder: Sorting Small Integers
// make a sorted permutation of
Procedure KSortArray´a,b : Array ½ of 0..K-1µ
½ of Æ
¼ ¼ : Array ¼
c // counters for each bucket
for := ½ to Ò do ·· // Count bucket sizes
C := 0
½ do ´
:= ¼ to Ã µ := ´ · µ// Store
for := ½ to Ò do // Distribute
Idea: Sort by starting node
Sanders: Graphs 19
Integrated Solution INFORMATIK
Ä ×Ø µ
¼ ¼ : Array ¼
¾ EdgeList do
foreach ´Ù ·· // count
for Ú := ½ to Ò do +=Î // preﬁx sums
¾ EdgeList do
foreach ´Ù // place
Úµ ÎÙ Ú
return ´Î µ
Example on Blackboard
Operations for Adjacency Arrays INFORMATIK
Navigation: easy. we will see several applications
Edge weights: becomes array of records
Incoming Edges: another (reverse) edge array
Delete Edges: explicit end indices
Batched Updates: rebuild
Sanders: Graphs 21
2.5 Adjacency Lists INFORMATIK
store (doubly linked) list of adjacent edges for each node
(a bit more restricted with singly linked lists)
· easy edge insertion
· easy edge deletion (order preserving)
more space (factor up to 3) than adj. arrays
more cache faults than adj. arrays
Enhancing Adjacency Lists INFORMATIK
For reverse edge access or edge weight updates in undirected graphs:
explicit edge objects
¾ stores weights
¾ pred/succ-pointers for all adjacency lists containing this edge
¾ reverse pointer for directed graphs
¾ cute trick: store only xor of incident nodes
Sanders: Graphs 23
Node insertion/deletion INFORMATIK
mark as deleted swap in node Ò list of nodes
µ node handles are pointers. 1
Problem: graphs with common node set
E list out list in list rev from to
(0,1) 0 0 0 V list first first deg deg
out in out in
0 0 0 0 2 0
0 2 2
0 00 2 2
0 0 0 0 0 2
(2,3) 0 0 0 0
¢ more space than vanilla adjacency arrays
2.6 Customization INFORMATIK
Customize data structure for application for maximum speed/compactness.
Sofware Engineering nightmare
Seperating algorithm from their representation may be a way out
Sanders: Graphs 25
Example: Recognizing dags INFORMATIK
¾ ¼ do delete Ú
¾ Use adjacency array storing incoming edges
¾ Store out-degrees
¾ Maintain a stack of out-degree zero nodes
¾ delete ´Ù by decrementing out-degree´Ùµ
¾ no explicit node deletion
¾ At the end test whether all nodes have out-degree´¼µ
2.7 Adjacency Matrix INFORMATIK
¼½ ´ µ ´ µ
· space efﬁcient for very dense matrices
space inefﬁcient otherwise. Exercise: what should “very dense” mean?
· Easy edge queries
Slow navigation (except for dense graphs)
·· joins linear algebra and graph theory
# -edge paths from to
Exercise: count -edge paths
Ç´ÐÓ µ mat-mults for mat-power
matrix multiplication in subcubic time, e.g., Strassen’s algorithm
Sanders: Graphs 27
Example where graph theory helps LA INFORMATIK
Ü ´½ ´ µ ¼µ
Problem: solve Consider Ò
Assume has two connected components
we swap rows and cols such that
¼ ½¼ ½ ¼ ½
Exercise: What if is a DAG?
2.8 Implicit Representation INFORMATIK
Compact representations of possibly very dense graphs
Implement algorithms directly using this representation
Sanders: Graphs 29
Example: Connectedness of Interval Graphs INFORMATIK
Idea: sweep intervals from left to right. The number of overlapping intervals
may never drop to zero.
Function isConnected(Ä : SortedListOfIntervalEndPoints) : ¼ ½
remove ﬁrst element of Ä
overlap := 1
foreach Ô do
¼ return 0
if Ô is a start point then overlap··
else overlap // end point
Ç´Ò ÐÓ Ç
algorithm for up to edges! Exercise: ﬁnd all connected
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.