Graphs - Tudor Girba

http://en.wikipedia.org/wiki/Glossary_of_graph_theory
Graphs
www.tudorgirba.com
http://en.wikipedia.org/wiki/Seven_Bridges_of_Königsberg
The city of Königsberg in Prussia was set on both sides of the Pregel River, and its four parts were
connected to each other by seven bridges.
Problem: Can we find a path through the city that would cross each bridge once and only once.
Picture from: http://en.wikipedia.org/wiki/File:Image-Koenigsberg,_Map_by_MerianErben_1652.jpg
The key to solve the problem was to see it as places connected by bridges.
http://www.math.dartmouth.edu/~euler/docs/originals/E053.pdf
An undirected graph (ungerichteter Graph), or simply a graph G = (V, E) consists of:
- a set V of nodes/vertices (Knoten), and
- a set E of edges (Kanten), connecting two distinct nodes: E = { {u,v} | u,v ∈ V}.
G = (V, E)
E = { {u,v} | u,v ∈ V}
e
a
c
d
b
g
f
For an edge {u,v} ∈ E in a directed graph, we say that:
- {u,v} is directed (orientiert) from u to v;
- u is the head (Kopf) of edge {u,v}.
- v is the tail (Ende) of the edge {u,v}.
A directed graph is shortly called Digraph.
A directed graph G = (V, E) is the binary relation E in VxV over the set of nodes V. A binary relation
E in VxV over the set of objects V defines a directed graph G=(V,E).
G = (V, E)
E = { {u,v} | u,v ∈ V}
e
a
c
d
b
g
A graph G = (V, E) is called directed (gerichtet) if its edges give directions (Orientierung) from one
node to another.
f
V = { a, b, c, d, e, f, g }
E = { {a,b}, {a,c}, {b,c}, {c,d}, {d,e}, {d,f}, {e,g}, {f,g} }
An undirected graph (ungerichteter Graph), or simply a graph G = (V, E) consists of:
- a set V of nodes/vertices (Knoten), and
- a set E of edges (Kanten), connecting two distinct nodes: E = { {u,v} | u,v ∈ V}.
G = (V, E)
E = { {u,v} | u,v ∈ V}
e
a
c
b
d
g
f
V = { a, b, c, d, e, f, g }
For a (undirected) graph G = (V, E), the binary relation E in VxV is symmetric. If {u,v} is an edge in
G, so is {v,u} an edge in G.
If E included in VxV is a symmetric relation, then the undirected graph Gʼ=(V,E) and directed graph
G=(V,E) are the same.
E = { {a,b}, {a,c}, {b,c}, {c,d}, {d,e}, {d,f}, {e,g}, {f,g} }
Only directed graphs can model antisymmetric /asymmetric/non-symmetric/ partial order relations.
a
b
c
d
e
f
g
a
0
1
1
0
0
0
0
b
0
0
1
0
0
0
0
c
0
0
0
1
0
0
0
d
0
0
0
0
1
1
0
e
0
0
0
0
0
0
1
f
0
0
0
0
0
0
1
g
0
0
0
0
0
0
0
a
b
c
d
e
f
g
a
0
1
1
0
0
0
0
b
1
0
1
0
0
0
0
c
1
1
0
1
0
0
0
d
0
0
1
0
1
1
0
e
0
0
0
1
0
0
1
f
0
0
0
1
0
0
1
g
0
0
0
0
1
1
0
Consider a graph G = (V, E), where V has n nodes.
The adjacency matrix (adjacency list, Adjazenzmatrix) of G is an n x n matrix A (that is, A has n
rows and n columns) such that:
- Au v=1 if {u,v} ∈ E, and
- Au v=0 if {u,v} ∈ E
e
a
c
d
b
g
f
Consider a graph G = (V, E), where V has n nodes.
The adjacency matrix (adjacency list, Adjazenzmatrix) of G is an n x n matrix A (that is, A has n
rows and n columns) such that:
- Au v=1 if {u,v} ∈ E, and
- Au v=0 if {u,v} ∈ E
e
a
c
d
b
g
f
The degree (grad) of a node u in V is the number of edges to which u incident is.
a
b
c
d
e
f
g
a
0
1
1
0
0
0
0
2
2
b
1
0
1
0
0
0
0
a
e
c
1
1
0
1
0
0
0
d
0
0
1
0
1
1
0
e
0
0
0
1
0
0
1
f
0
0
0
1
0
0
1
g
0
0
0
0
1
1
0
2
2
3
3
2
2
2
b
c
d
3
3
For a undirected graph, you compute the degree of a node by summing up the column
corresponding to the node.
Degree(a) = Sum(A(a, yi)) , ∀yi ∈ V
g
f
2
Degree of a node
2
For a directed graph, you compute the degree of a node by summing up the column AND the row
corresponding to the node.
Degree(a) =" Sum(A(xi, a)) ∀xi ∈ V +
" Sum(A(a, yi)) ∀yi ∈ V
A graph G = (V, E) is called weighted (gewichtet) when a weight/label (Gewicht/Attribut) is
associated with every edge in the graph.
a
b
c
d
e
f
g
a
0
2
3
0
0
0
0
b
0
0
1
0
0
0
0
c
0
0
0
2
0
0
0
d
0
0
0
0
5
4
0
e
0
0
0
0
0
0
3
f
0
0
0
0
0
0
3
g
0
0
0
0
0
0
0
a
3
c
2
b
5
2
e
3
d
g
4
1
f
3
Weighted graphs
Not complete
a
Complete
A graph G = (V, E) is called complete (vollständig) when every two distinct nodes is connected by
an edge.
a
c
c
b
b
a
a
For all a,b in V, there exists e in E such that e = {a, b}.
In a complete undirected graph with n nodes, the degree of every node is n-1.
c
b
c
b
A graph G = (V, E) is called bipartite (bipartit) if:
- its nodes can be divided into two disjoint sets U and W
- its edges only connect a node from U with a node from W.
G = (V, E)
∀ e={v,w} ∈ E, v ∈ V and w ∈ W.
Bipartite
Not bipartite
( V=U ∪ W, U∩W=Ø );
For all e={v,w} in E, v in V and w in W.
Path
Consider a graph G = (V, E).
- A path/way (Pfad/Weg) in a graph is a sequence of nodes k nodes such that each node and the
next node are connected by an edge:
(u1, u2, ..., uk) u1,...,uk in V
ui-1
Cycle
e
a
c
d
g
b
The Length (Länge) of the path (u1, u2, ..., uk) is k-1.
f
The path (u1, u2, ..., uk) is a cycle (Zyklus, Kreis) if: u1 =uk and the length of the path is ≥3 (that is
k≥4)
Path: (b, a, c); Length (b, a, c) = 2
Path: (b, d, f)
Cycle: (f, g, e, d, f); Length (f, g, e, d, f) = 4
If the graph G has one or more cycles, then it is called a cyclic (zyklisch) graph. A graph with no
Consider a graph G = (V, E).
An edge connecting a node u with the node u itself is called a loop (Schlaufe).
Loop-free
Loop
Loop-free graph: (a) is a path of length 0 (a,a) is not a path
c
d
b
e
a
e
a
c
g
d
b
f
g
Graph with a loop: (a) is a path of length 0 (a,a) is a path of length 1 {a,a} is a loop
f
A reflexive binary relation can be modelled with a graph with loops on each node. An irreflexive
binary relation can be modelled with a loop-free graph. For a complete and loop-free graph G=
(V,E): ∀u,v: u,v in V: u ≠v => {u,v} in E.
http://en.wikipedia.org/wiki/Eulerian_path
A path is called an eulerian path (Euler-Pfad) if:
- it contains all edges of the graph;
- each edge is contained only once.
Eulerian path
e
a
c
b
d
g
f
e
a
c
b
d
g
f
An eulerian path that is a cycle is called an eulerian cycle (Euler-Kreis).
http://en.wikipedia.org/wiki/Hamiltonian_path
Hamiltonian path
e
a
c
e
a
d
g
b
c
f
d
b
Components
e
e
c
d
d
g
b
g
f
f
a
G = (V, E).
T ⊆ E.
a
A cycle is a hamiltonian cycle (Hamilton-Kreis) if:
- it contains all nodes of the graph;
- each node is contained only once, except the start and end node u1 which is contained exactly
twice.
Consider a graph G = (V, E).
The subset T Œ E is a spanning tree (spannender Baum) of G if:
- every node in V belongs to an edge of T;
- between every two distinct nodes of G there is a path in T;
- edges of T form no cycles.
The subset T Œ E is a component (Komponent) of G if: between every two distinct nodes
belonging to some edges of T there is a path in T.
c
Critical node
c
b
g
f
Spanning tree
a
A path is called a hamiltonian path (Hamilton-Pfad) if:
- it contains all nodes of the graph;
- each node is contained only once.
Eulerian path
e
d
Critical edge
A node uœV in the graph G is critical (kritisch) if by deleting u from G the graph G is divided into
not connected components.
- An edge {u,v}œE in the graph G is critical (kritisch) if by deleting {u,v} from G the graph G is
divided into not connected components.
- Critical nodes and edges of the graph G form the articulation points (Artikulationspunkte) of G.
g
f
A node u in V in the graph G is isolated (isoliert) if it is the only node of a component of G.
A component TŒ E of G is a biconnected component (zweifach zusammenhängend), if - by
deleting an arbitrary node from T, - the remaining nodes and edges in T still form a component of
G.
Biconnected components
e
a
c
d
g
b
f
The graph G1=(V1,E1) is a subgraph (Subgraph) of G, if
V1 included in V and
E1={{u,v} in E, for all u,v in V1 } included in E.
G = (V, E)
G1 = (V1, E1)
E1 = {{u,v}∈ E | u,v ∈ V1} ⊆ E.
e
a
c
Subgraph
d
g
b
Not subgraph
f
Weakly reachable = exists undirected path
e
a
c
b
d
g
f
Strongly reachable = exists directed path
- G1 = {V1,E1} is a subgraph, where: V1 = {a,b,c} T1 = { {a,b}, {a,c}, {b,c}}
- G2 = {V2,E2} is not a subgraph, where: V2 ={d,e,f,g} T2 = { {d,e}, {e,g}, {g,f}}
9
A classic problem is finding the shortest path in a space of connected places. Such a space can
be represented as a graph in which each two nodes are connected via edges that have different
weights or costs. For example, such a problem can help us find:
- the cheapest route between two places
- the fastest route between two places
F
E
6
2
C
14
11
D
One possibility would be to take the backtracking approach, try all possibilities and then pick the
shortest. However, that is not necessarily optimal.
9
15
10
A
7
B
orithm
stra alg
le: Dijk
p
m
a
x
E
http://scg.unibe.ch/download/lectures/ei/01ComputationalThinking.pptx
Another way is to start from the start node and then traverse each reachable nodes from that node
to compute intermediary costs. At each step we check if the temporary cost in the target node is
less than the cost of reaching the current node plus the direct way. If itʼs less, we replace the
temporary cost with the new one.
∞
9
∞
Example taken from:
http://scg.unibe.ch/download/lectures/ei/01ComputationalThinking.pptx
F
E
6
2
∞
∞
C
14
11
This is called the Dijkstra algorithm:
http://en.wikipedia.org/wiki/Dijkstra's_algorithm
D
9
15
10
0
A
7
B
∞
thm
a algori
str
le: Dijk
Examp
∞
9
14
F
E
6
2
9
C
14
∞
11
D
9
15
10
0
A
7
B
7
thm
a algori
str
le: Dijk
Examp
∞
9
14
F
E
6
2
7 + 15 = 22
9 < 7 + 10
11
D
C
14
9
15
10
0
A
7
B
7
thm
a algori
str
le: Dijk
Examp
∞
9
14 > 9 + 2
F
E
6
2
9
C
14
22 > 9 + 11
11
D
9
15
10
0
A
7
B
7
thm
a algori
str
le: Dijk
Examp
20
9
11
F
E
6
2
9
C
14
20
11
D
9
15
10
0
A
7
B
7
thm
a algori
str
le: Dijk
Examp
20 < 20 + 6
9
11
F
E
6
2
9
C
14
20
11
D
9
15
10
0
A
7
B
7
thm
a algori
str
le: Dijk
Examp
a b c d e f g
a 0 2 3 0 0 0 0
a
3
c
2
b
5
1
2
e
b 0 0 1 0 0 0 0
3
d
g
4
f
3
c 0 0 0 2 0 0 0
d 0 0 0 0 5 4 0
e 0 0 0 0 0 0 3
f 0 0 0 0 0 0 3
The Floyd Warshall algorithm determines the smallest distance between any two nodes in the
graph. The essence of the algorithm is given by the observation that the minimum distance
between i and j is the minimum between:
- the known distance between i and j, and
- the distance between i and some k + the distance between k and j.
g 0 0 0 0 0 0 0
procedure FloydWarshall ()
for k := 1 to n
for i := 1 to n
for j := 1 to n
path[i][j] = min ( path[i][j], path[i][k]+path[k][j] );
hall
d Wars
le: Floy
Examp
Exercise: Code the above algorithm in your favorite language and check the results
The traveling salesman is another classic graph problem: Find the shortest path for a salesman
that has to go from his home city to a series of other cities and back.
In essence, we need to find an the shortest Hamiltonian cycle in a undirected graph.
http://en.wikipedia.org/wiki/Travelling_salesman_problem
le:
Examp
g sales
Travelin
man
Tudor Gîrba
www.tudorgirba.com
creativecommons.org/licenses/by/3.0/