Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Birgit Vogtenhuber Simple Graph Algorithms1
Simple Graph Algorithms
Design & Analysis of Algorithms WS 2019/20
Birgit Vogtenhuber
Birgit Vogtenhuber Simple Graph Algorithms2
Why Graph Algorithms?
• Graphs are a versatile data structure
• Graph-related algorithms are rather important inalgorithm theory and applications
• Many computational problems can be formulatedefficiently in terms of graphs
Birgit Vogtenhuber Simple Graph Algorithms3
Plan
• some basic graph terminology
• possibilities to store graphs
• some simple graph algorithms
Birgit Vogtenhuber Simple Graph Algorithms4 ii
Basic Terminology
• Directed graph G = (V,E): V is the set of vertices andE is the set of edges: E ⊆ V × Vedge (u, v): ordered pair of ordered pair of vertices,
u = startpoint, v = endpoint
Example: V = 1, 2, 3, 4, 5,E = (1, 2), (2, 3), (3, 2), (4, 5), (4, 4)
1 2 4 5
3
• A loop at a vertex v ∈ V is an edge (v, v)• A directed Graph is called simple if it has no loops
Birgit Vogtenhuber Simple Graph Algorithms5 iii
Basic Terminology
• Undirected graph: like a simple directed graph whereE is symmetric, that is, (v, w) ∈ E ⇔ (w, v) ∈ E
• A (directed or undirected) weighted graph is a tripleG = (V,E, g) where g assigns a weight to each edge
25
3
82
11
33 4
16
7 5
3
8
⇔ E is a set of unordered pairs (v, w) with v 6= w ∈ V
Birgit Vogtenhuber Simple Graph Algorithms5 x
Basic Terminology
• A path in G = (V,E) from v to w is a sequenceof vertices v = v0, v1, . . . , vk = w ∈ Vwith edges (vi, vi+1) ∈ E for all 0≤ i< k
• Length of a path: number of its edges (vi, vi+1)
length in weighted graphs: sum of the edge weights
• Simple path: contains every vertex in V at most once
v
w
• Distance dG(v, w) from v to w: length of the shortestpath from v to w in G (or ∞ if no such path exists)
Birgit Vogtenhuber Simple Graph Algorithms5 xvi
Basic Terminology
• A cycle in G=(V,E) is a path v0, v1, . . . , vk with v0 =vk
• Simple cycle: contains every vertex in V at most once,except for v0 = vk
• Trivial cycle: contains only one vertex:or two for undirected graphs:
• Length of a cycle: number of its edges (vi, vi+1)
length in weighted graphs: sum of the edge weights
• Length of a cycle: number of its edges (vi, vi+1)
length in weighted graphs: sum of the edge weights
Birgit Vogtenhuber Simple Graph Algorithms6
Basic Terminology
• a subgraph G′ = (E′, V ′) of G = (E, V ) is a graphwith V ′ ⊆ V and E′ ⊆ E
Example :Graph G Subgraph G′
Birgit Vogtenhuber Simple Graph Algorithms7 ii
Basic Terminology
• A graph is acyclic if does not contain any subgraphthat is a non-trivial simple cycle
• A graph is connected if it contains a path from v to wfor every pair v, w ∈ V (not necessarily an edge (v, w))
connected : disconnected :
acyclic : not acyclic :
disconnected :
Birgit Vogtenhuber Simple Graph Algorithms8 ii
Basic Terminology
• An undirected graph is a forest if it is acyclic
• A an undirected grpah is a tree if it is acyclic andconnected
Birgit Vogtenhuber Simple Graph Algorithms9 iii
Basic Terminology
• The in-degree (out-degree) of a vertex v ∈ V is thenumber of edges ending (starting) in v:
in-degree (v) := |w | (w, v) ∈ E|out-degree (v) := |w | (v, w) ∈ E|
• For undirected graphs we have
in-degree(v) = out-degree(v) ∀ v ∈ V .
In this case it called the degree of the vertex v.
Degrees of vertices in a graph G = (V,E):
Birgit Vogtenhuber Simple Graph Algorithms10 ii
Basic Terminology
• degree (v) = 0: v is called an isolated vertex of G
• degree (v) = 1: v is called a leaf (end vertex) of G
• degree (v) = r ∀ v ∈ V : The graph G is called aregular graph of degree r
Example: regular graph(s) of degree 3
Degrees of vertices in an undirected graph G = (V,E):
Birgit Vogtenhuber Simple Graph Algorithms11 ii
Basic Terminology
• An empty graph has no edges (regular of degree 0)
K6
• A complete graph on n vertices contains all(n2
)possible edges (regular of degree n− 1)
K4
Degrees of vertices in an undirected graph G = (V,E):
Birgit Vogtenhuber Simple Graph Algorithms12 iii
How to Store a Graph?
⇒ Analysis wants two parameters !
• We distinguish between
dense graphs: m ≈ n2, for example complete graphs
sparse graphs: m n2, for ex. trees (m = n− 1)or hypercubes (m = d
2 · n = O(n log n), where d isthe dimension of the hypercube)
• Size of a graph G = (V,E):
Number of vertices: n = |V | Number of edges: m = |E|, 0 ≤ m ≤ n2
⇒ In total: size Θ(n+m)
Birgit Vogtenhuber Simple Graph Algorithms13 ii
How to Store a Graph?
Adjacency-matrix: Matrix A[1 . . . n, 1 . . . n] with
A[i, j] =
1 if (i, j) ∈ E0 else
• Memory: Θ(n2)• convenient for dense graphs• test for existence of edge in Θ(1) time
Adjacency-List: Array F [1 . . . n] with pointers,F [i] points to a linear list with all vertices that are incident
to the ith vertex
• Memory: Θ(n+m)• test for existence of edge in Ω(1), O(n) time
Birgit Vogtenhuber Simple Graph Algorithms14
How to Store a Graph?
1 2
34
A =
0 1 0 01 0 1 10 1 0 10 1 1 0
1 2 3 4
1
2
3
4
F : 1 2 3 4
2 1
3
4
2
4
2
3
↑ symmetric if graph is undirected
← 1 Entry for every node,Θ(n+m) memory for all edges.
The k neighbours of a node canbe obtained in Θ(k) time.
A small example:
Birgit Vogtenhuber Simple Graph Algorithms15 ii
Searching in Graphs
• Known from Data Structures and Algorithms (1):
Searching in binary trees
“Walking” through a binary tree:- in-order (symmetrische Reinehfolge)- pre-order (Hauptreihenfolge)- post-order (Nebenreihenfolge)
• Searching in general graphs:
Breadth-first search (BFS):search closeby vertices first
Depth-first search (DFS)search one branch first
Birgit Vogtenhuber Simple Graph Algorithms16 vi
Breadth First Search - BFS
Given: connected graph G = (V,E), startnode s ∈ VIdea: Starting from s, search in all directions uniformly
• visit vertices u with distance dG(s, u) = 1,then with dG(s, u) = 2, and so on.
• traverse a BFS-tree T with root s:branches of T consist of shortest paths to s.
• to build T , assign the following values to each vertex u:
pre(u) predecessor of u in the BFS-Tree Tstate(u) new (unvisited), labelled (visited),
satured (all neighbours visited)
• store visited unsaturated vertices in a queue Q
• initially: Q empty, pre(u) not set, state(u) =new
Birgit Vogtenhuber Simple Graph Algorithms17
BFS Algorithm: Pseudo-Code
BFS(G,s) /* G given as adjacency list F */for all u ∈ V
state(u)=newstate(s)=labellednum(s)=1; i=2; pre(s)=nilPUT(Q,s)while Q 6= 0
GET(Q,u)for all v ∈ F [u] /* testing all neighbors of u */
if state(v)==newstate(v)=labelled; num(v)=ipre(v)=u; PUT(Q,v); i = i+ 1
state(u)=satured
Birgit Vogtenhuber Simple Graph Algorithms18 vii
BFS Algorithm: Analysis
Runtime:
• Each vertex is inserted into Q exactly once:Θ(1) time per vertex ⇒ Θ(n) for all vertices
• After removal of a vertex u from Q, the algorithm goesthrough the adjacency-list of u:Θ(degree(u)) time for u ⇒ How much for all vertices?
Every edge contributes to exactly two lists⇒∑
u∈V degree(u) = 2m ⇒ Θ(m) for all vertices
⇒ The whole algorithm: Θ(n+m) time in total
Memory: Θ(n) for Q, vertices ⇒ Θ(n+m) space in total
⇒ Runtime and memory linear in the size of G
Birgit Vogtenhuber Simple Graph Algorithms19 vi
All Distances in an Unweighted Graph
Question: How can one compute of all distances betweenpairs of vertices in an unweighted graph G ?
• All the shortest paths from some vertex u to s arecoded in the BFS-tree T via the pre-pointers.
• distances to s can be easily computed during BFS: dG(s, s) = 0 dG(s, u) = dG(s, pre(u)) + 1
⇒ Running BFS for n times (once for each vertex), onecan compute the distances between any u, v ∈ V
⇒ The distance-matrix of G can be computed in Θ(n ·m)time and Θ(n2) space if G is connected.
? What if G is disconnected?
Birgit Vogtenhuber Simple Graph Algorithms20 iv
Bipartite Graphs
A graph G(V,E) is called bipartite, if there exists apartition of V into V1, V2 such that all (u, v) ∈ E have oneendpoint in V1 and the other one in V2
V1 V2
In other words:G contains no edges within V1 or V2
Birgit Vogtenhuber Simple Graph Algorithms21 vi
k-Colorability of a Graph
A graph G(V,E) is called k-colorable if its nodes can becolored with ≤ k colors, so that no nodes of the samecolor share an edge.
k = 3 colors are enough
The chromatic number χ(G) (spoken ’Chi of G’) of agraph G is the minimum k such that G is k-colorable.
Example: How many colors are needed?
Examples:
χ(cycle) =
2, n even3, nodd
χ(K4) = 4
Birgit Vogtenhuber Simple Graph Algorithms22 iv
Recognizing Bipartite Graphs
Observation: A graph G is 2-colorable ⇔ G is bipartite
Algorithm:• Choose arbitrary vertex s and color it blue• Traverse G in BFS-Order, starting from s• For each vertex u that is removed from the queue Q: u is colored red (blue) check for all colored neighbors that they are blue
(red). If no: return false color all its uncolored neighbors in blue (red)
• After processing all vertices: return true.
Question: How to determine whether a graph is bipartite?
Questions: Correctness? Runtime & Memory?
Birgit Vogtenhuber Simple Graph Algorithms23 v
Depth First Search - DFS
• Idea:DFS explores the graph, starting at the last visitedvertex having unvisited neighbors.
• Special case: G is a tree ⇒ DFS-Order = pre-order
a
b
d e
c
f g h
i j
• Maintain Stack STthat contains allvisited but not yetsatured nodes.
• Rest similar to BFS
Question: What is the pre-order for this tree?
a b d e c f i j g h
Birgit Vogtenhuber Simple Graph Algorithms24 ii
DFS Algorithm: Pseudo-Code
DFS(G) /* G given as adjacency list F */for all u ∈ V
state(u)=newpre(u)=nil
for all u ∈ V /* loop not necessary for connected graphs */if state(u)==new
DEPTH(u)
DEPTH(u)state(u)=visited; write(u)for all v ∈ F [u] /* test all neighbors of u */
if state(v)==newpre(v)=uDEPTH(v) ← recursion can be replaced by stack
state(u)=saturated
Birgit Vogtenhuber Simple Graph Algorithms25 iii
DFS Algorithm: Example
1 2 3
5 6 7
root
Example: F : 1 2 3 4
1
6
6
7
8
5 6 7 8
2
3
5
3 4
4
8
2
5
1
2
6
root1
6
arrows point to predecessors
Birgit Vogtenhuber Simple Graph Algorithms25 iv
DFS Algorithm: Example
Further Observations:
• The pre-pointers form a set of trees (DFS-forest);
• every call of DEPTH in the main programm results in anew root and tree.
• For connected graphs there is only one root and tree.
1 2 3
5 6 7
root
Example: F : 1 2 3 4
1
6
6
7
8
5 6 7 8
2
3
5
3 4
4
8
2
5
1
2
6
root1
6
Birgit Vogtenhuber Simple Graph Algorithms26 ii
DFS Algorithm: Analysis
Runtime Analysis:
• DEPTH ist called exactly once per node (only for newnodes, that are immediately marked as ”visited”).
• A call of DEPTH(v) takes O(degree(v)) time
⇒ Θ(n+m) time in total
Space: Θ(n+m) space in total
Correctness:• vertex that is set to visited: put on stack when removed from stack, all neighbors are considered
⇒ every vertex set to visited exactly once
Birgit Vogtenhuber Simple Graph Algorithms27
Next
Planar and Plane Graphs:
Definition, Recognition, Properties
Birgit Vogtenhuber Simple Graph Algorithms28 ii
Isomorphic Graphs
Two graphs G = (V,E) and G′(V ′, E′) are calledisomorphic if there exists a bijection φ : V → V ′, so that(v, w) ∈ E ⇔ (φ(v), φ(w)) ∈ E′
2 3
1 4
Example:
4
1
2
3
⇔
5 55
Birgit Vogtenhuber Simple Graph Algorithms29 ii
Planar Graphs
Definition 1:A graph G = (V,E) is called planar if there exists aninjective mapping of V to n = |V | distinct points in theEuclidian plane and a mapping of E to pairwise disjointopen curves in the Euclidian plane, so that:
1. e = (xi, xj)⇒ curve (e) connects point (xi) withpoint (xj)
2. e = (xi, xj)⇒ curve (e) does not contain any otherpoint (xk), k 6= i, j
Definition 2:A graph is called planar if it can be drawn in the planewithout intersections between its edges (except in endpoints).
Birgit Vogtenhuber Simple Graph Algorithms30 ii
Plane & Embeddable Drawings
A drawing of a graph is called plane if it is free ofintersections.
A non-plane drawing of a graph is called embeddable if itis isomorphic to a planar graph.
A plane graph is a plane drawing of a planar graph.
2 3
1 4
4
1
2
3
⇔2 3
1 4
Example of an embeddable / plane drawing:
4
1
2
3
⇔
Birgit Vogtenhuber Simple Graph Algorithms31 iii
Recognizing Planar Graphs
Examples of non-embeddable drawings:
K3,3 K5
K3,3 K5
Non-embeddable drawing ⇔ drawing of a non-planar graph
Birgit Vogtenhuber Simple Graph Algorithms31 iv
Recognizing Planar Graphs
Examples of non-embeddable drawings:
K3,3 K5
K3,3 K5
Embeddable drawing ⇔ drawing of a planar graph
Birgit Vogtenhuber Simple Graph Algorithms31 ix
Recognizing Planar Graphs
Examples of non-embeddable drawings:
K3,3 K5
Kuratowski’s Theorem: A graph G is planar if and only ifit does not contain a subgraph G′ that is isomorphic to asubdivision of K3,3 or K5, respectively. [ without proof ]
Subdivision of a graph G: some vertices are added“on edges” of G (some edges of G replaced by paths)
additional vertices: degree 2
embeddability is invariant undersubdivision of edges
Birgit Vogtenhuber Simple Graph Algorithms32 viii
Plane Graphs: Euler’s Formula
Observation:A plane graph (= plane drawing of a planar graph) dividesthe plane into regions called the faces of the drawing
Theorem: Euler’s Formula (on plane graphs)Given a connected, plane graph with exactly f faces,n = |V |,m = |E|. Then it holds that:
n+ f = m+ 2In other words: vertices - edges + faces = 2
4 faces
12 edges
10 vertices
10+4=12+2 X
Birgit Vogtenhuber Simple Graph Algorithms33 iv
Planar Graphs: Example
1011
12
13
149
8
76
3
2
4
15
Vertices: n = 14Edges: m = 13Faces: f = 1
14 + 1 = 13 + 2 X
Birgit Vogtenhuber Simple Graph Algorithms34 v
Planar Graphs: Example
1
24
3
78
69
1011
12
14
1715
5
18
13
Vertices: n = 18Edges: m = 21Faces: f = 5
16
18 + 5 = 21 + 2 X
Note: All plane drawings of the same graph have thesame number of faces
Birgit Vogtenhuber Simple Graph Algorithms35 iv
Planar Graphs
Theorem: For a planar connected graph G = (V,E) with|V | = n ≥ 3 and |E| = m it holds that m ≤ 3 · n− 6
Proof: Consider a plane drawing of G with f faces. Letei, i = 1, . . . , f be the number of edges which bound face i.
•∑f
i=1 ei ≥∑f
i=1 3 = 3 · f (since ei ≥ 3)
•∑f
i=1 ei = 2 ·m(since each edge is counted twice – once from each side)
⇒ Combine the two results: 3 · f ≤ 2 ·m⇒ Combine with Euler’s Formula (multiplied by 3):
3 ·m+ 6 = 3 · n+ 3 · f ≤ 3 · n+ 2 ·m⇔ m ≤ 3 · n− 6 X
Birgit Vogtenhuber Simple Graph Algorithms36 iii
Non-Planarity of K5
Claim: K5 is not planar.
Proof:• K5 has n = 5 vertices and m =
(52
)= 10 edges
• Assume K5 is planar
⇒ We can apply the previous theorem
m ≤ 3 · n− 6, with n = 5,m = 10
10 ≤ 3 · 5− 6 = 9 contradiction!
⇒ K5 cannot be planar. X
Question:Can we show in the same way that K3,3 is not planar?
Birgit Vogtenhuber Simple Graph Algorithms37 ii
Non-Planarity of K3,3
Theorem: For a bipartite planar connected graph G = (V,E)with |V | = n ≥ 3 and |E| = m it holds that m ≤ 2 · n− 4
Proof: Consider a plane drawing of G with f faces. Letei, i = 1, . . . , f be the number of edges which bound face i.
• G bipartite ⇒ every face is 2-colorable ⇒ ei ≥ 4
•∑f
i=1 ei ≥∑f
i=1 4 = 4 · f ,∑f
i=1 ei = 2 ·m⇒ Combine the two results: 4 · f ≤ 2 ·m ⇔ 2 · f ≤ m⇒ Combine with Euler’s Formula (multiplied by 2):
2 ·m+ 4 = 2 · n+ 2 · f ≤ 2 · n+m⇔ m ≤ 2 · n− 4 X
Birgit Vogtenhuber Simple Graph Algorithms37 v
Non-Planarity of K3,3
Claim: K3,3 is not planar.
Proof:• K3,3 has n = 6 vertices and m = 3 · 3 = 9 edges
• Assume K3,3 is planar
⇒ We can apply the previous theorem
m ≤ 2 · n− 4, with n = 6,m = 9
9 ≤ 2 · 6− 4 = 8 contradiction!
⇒ K3,3 cannot be planar. X
Question:Can we use Euler’s formula to show how many edges andtriangles a triangulation of n points in the plane has ?