Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Directed graph: (V,E) such that
V is a set of vertices (or nodes)
E V V is a set of edges (or arcs)
a
b
c
d
e
in-degree = 2
out-degree = 1
V = {a,b,c,d,e}
E = { (b,a), (c,b),
(d,c),(e,d),(e,b),
(b,e),(e,a) }
Undirected graph: (V,E) such that
V is a set of vertices
E is a set of unordered edges
(i.e. unordered pairs of vertices)
sets of size 2
b
c
d
e
degree = 3
V = {a,b,c,d,e}
E = { {a,b}, {b,c},
{c,d},{d,e},{e,a},
{e,b} }
a
a is adjacent to b
• networks are graphs
• object hierarchies are graphs
• circuit layouts are graphs
complete graph on n vertices (Kn):
undirected graph containing an edge
between each pair of distinct vertices
Def
init
ion
...
bipartite graph: (V,E)
an undirected graph whose vertex set V can be partitioned
in two disjoint, nonempty sets V1 and V2 such that every
edge connects a vertex in V1 to a vertex in V2.
Def
init
ion
V1 V2 V1 V2
b
c
d
ea
1. Edge list: {a,b}, {b,c}, {c,d},{d,e},{e,a}, {e,b}
size:
adjacency time:
2. Adjacency list:
size:
adjacency time:
V vertices
E edges
Vertex Adjacencies
a b, e
b a, c, e
c b, d
d c, e
e a, b, d
3. Adjacency matrix:
b
c
d
ea
a b c d e
a 0 1 0 0 1
b 1 0 1 0 1
c 0 1 0 1 0
d 0 0 1 0 1
e 1 1 0 1 0
size:
adjacency time:
4. Incidence matrix:
1 2 3 4 5 6
a 1 1 0 0 0 0
b 0 1 1 0 1 0
c 0 0 0 0 1 1
d 0 0 0 1 0 1
e 1 0 1 1 0 0
12
34
56
size:
adjacency time:
A=
A2 ... A
Graph G1=(V1,E1) is isomorphic to G2=(V2,E2) iff
•(intuitive) can relabel vertices of G2 to get G1
•(mathematical) one-to-one function f: V1V2
with (a,b)E1 (f(a),f(b))E2
Def
init
ion
1 2
3 4
1 2
4 3~
isomorphic
Path (in an undirected graph):
a sequence of edges joined end-to-end
(usually denoted by their vertices, in order)
Simple path: path that does not use the same edge
more than once.
Def
init
ion
5 5
e.g. ({1,3},{3,4},{4,2},{2,5}) = <1,3,4,2,5>
Cycle: path that starts and ends at the same vertex:
Def’n
e.g. <5,2,4,3,1,2> but not <5,2,4,3,1,2,5>
Def’n
e.g. <1,2,4,3,1> or <5,2,4,3,1,2,5>
Def’n A path is Eulerian if it covers every edge in the
graph exactly once.
(if a graph has an Eulerian path, then you can
draw it without lifting your pencil)
2
1
4
3 5 6
7
8Eulerian path:
<1,2,3,4,5,6,7,8>
1 2
3 4
5
1 2
3 4
5
can replace
path with cycle
Theorem: Let G=(V,E) be an undirected graph. Then
EvVv
2)(degree
Why? every 1 edge connects 2 vertices
1 2
3 4
5vertex degree
1 4
2 4
3 3
4 3
5 2
sum = 4+4+3+3+2 = 16
# of edges = 8
Theorem: Let G=(V,E) be an undirected graph. Then
EvVv
2)(degree
Corollary: Always have an even # of vertices
with odd degree.
A graph is connected if there is a path
between every pair of distinct vertices.
Def
init
ion
1 2
3 4
5
1 2
3 4
5
Which graphs have
Eulerian
paths/cycles?
A graph has an Eulerian cycle iff it is connected
and every vertex has even degree.
A graph has an Eulerian path iff it is connected
and exactly two vertices have odd degree.
Th
eore
m
A brief sketch of the proof
Eulerian cycle all even degrees:
For a cycle, when enter vertex along one edge,
we must immediately leave along another edge:
1 2
3 4
5
Thus, for every vertex, #entrances = # exits.
degree(vertex) = #entrances + #exits = 2*#exits
is even.
For path, start and end vertices do
not to have even weight.
all even degrees Eulerian cycle
proof by algorithm:
1. start at an arbitrary vertex
2. wander around until stuck
1 2
3 4
5
we can only get stuck at the start vertex, since
wandering into a vertex reduces its available
edges by 1, making it odd so it has at least
one more available edge.
3. if edges are left over, do same with
a vertex with unused edge and splice
resulting cycles.
1 2
3 4
5
1 2
3 4
5
1 2
3 4
5
Def’n A path is Hamiltonian if it touches every vertex in
the graph exactly once.
A Hamiltonian circuit comes back to original vertex.
2
1
4
3 5 6
7
8Hamiltonian:
<1,2,3,4,5,6,7,8>
1 2
3 4
5
1 2
3 4
5
A graph has Hamiltonian circuit if
•it is connected
•has n3 vertices
•every vertex has degree n/2Th
eore
m
In general: Very hard to find Hamiltonian path.
Def
init
ion
A graph is planar if it can be drawn on a flat
piece of paper so that no two edges cross.
non-planar drawing
of a planar graph
a b
cd
planar drawing
of a planar graph
Useful for:
1. VLSI design (overlapping edges require
extra layers)
2. circuit design (cannot overlap wires on board)
through isomorphism
(by Kuratowski - prove for 0.3pts extra)
A graph is planar iff it does not contain a
subdivision of K5 or K3,3.Th
eore
m
V+F = E + C + 1
V = # of vertices
E = # of edges
F = # of faces
C = # of connected components
V=11 E=13 F=6 C=3
V=10 E=12 F=5 C=2
Def
init
ion
A tree is a connected, acyclic graph.
path between any two vertices
no cycles (i.e. circuits)
Def
init
ion
A forest is an acyclic graph.
Th
eore
m A graph is a tree iff there is exactly one path
between any two vertices.
Th
eore
m
Every tree with n vertices has n-1 edges.
rootlevel 0
level 1
level 2
level 3
a
b c
parent of c
parent of b
child of a
sibling of c
child of a
sibling of b
•All edges directed away from root.
•Depth (or level) is distance from root.
•Leaves are nodes with no children.
•Every node (except root) has 1 parent
(CS trees)
Def’n A binary tree is a rooted tree where every node
has at most 2 children.
root
Left
subtree
Right
subtree
In a full binary tree (complete), every internal node
has 2 children.
internal node
(i.e. non-leaf)
a
b e
c d f
g
Preorder:
1. visit the root
2. recursively traverse left subtree
3. recursively traverse right subtree
In order:
1. recursively traverse left subtree
2. visit the root
3. recursively traverse right subtree
Postorder:
1. recursively traverse left subtree
2. recursively traverse right subtree
3. visit the root
+
- *
2 4 +
1 3
Preorder: + - 2 * 4 + 1 3 (Lisp, Scheme)
In order: -2 + 4 * 1 + 3 (C, C++, Java)
Postorder: 2 - 4 1 3 + * + (HP, PS, Forth)
Binary Search Tree: left <= root <= right
•in order traversal gives sorted list
•easy to search