discrete math for cs chapter 7: graphs. discrete math for cs map of koenigsberg at the time of euler
Post on 21-Dec-2015
232 views
TRANSCRIPT
Discrete Math for CS
Chapter 7: Graphs
Discrete Math for CS
Map of Koenigsberg at the time of Euler
Discrete Math for CS
Koenigsberg Bridge Problem
Find a path starting and stopping at the same place and crossing each bridge once and only once.
Discrete Math for CS
Euler Graph
An Eulerian Graph is a graph (TBD) for which there is a solution to the KBP (a path that starts and stops at the same vertex and uses each edge once and only once).
The path is called the Eulerian Path.
Euler's observation: In constructing an Eulerian Path, each time you come to a vertex by one edge you must leave it by a different edge.
Consequence: If an Eulerian Path exists for a graph there must be an even number of edges meeting at each vertex.
KBP: No solution. Proof: Contrapositive.
Discrete Math for CS
Graph
A graph consists of vertices and edges – G = (V,E).
The degree of a vertex (written (v)) is the number of edges incident to v.
The KBP graph has 4 vertices with an odd number of incident edges.
Discrete Math for CS
Simple Graph
A simple graph is a graph with no loops (no vertex is joined to itself by an edge and no multiple edges (two or more edges joining the same two vertices.
Two vertices are said to be adjacent if there is an edge between them (incident to them both).
The edges of a graph can be viewed as a set of pairs of vertices.
Thus the edges form a relation on the set of vertices and in the case of a simple graph this relation is irreflexive and symmetric.
The matrix formed to represent the edges of a simple graph (seen as a relation) is called the adjacency matrix of the graph.
Discrete Math for CS
Exercise:
Build the adjacency matrix
a
b c
e d
a b c d e
a
b
c
d
e
0 1 0 0 1
1 0 1 1 0
0 1 0 0 1
0 1 0 0 1
1 0 1 1 0
Discrete Math for CS
Subgraph
A subgraph of a graph G = (V,E) is any graph G' = (V',E') where V' V and E' E.
Discrete Math for CS
Exercise:
Find the subgraphs of G.
G H
K L
Discrete Math for CS
Paths:
A path of length k in a graph is a sequence of distinct vertices v
0, v
1, ..., v
k such that v
i-1v
i is an edge for i = 1...k.
We denote such a path as v0v
1...v
k .
A cycle is a path, v0v
1...v
k , in a graph where v
0 = v
k.
Exercise: Find the cycles in the graph, G, of the previous example
G 5-cycles:
4-cycles:
3-cycles:
Discrete Math for CS
Acyclic Graph:
A graph with no cycles is called acyclic.
Trees are typical acyclic graphs
Discrete Math for CS
Connected Graph:
A graph is connected if there is a path between every pair of vertices.
In general, a graph can be partitioned into subgraphs, each of which is connected.
The minimum number of connected subgraphs of a graph is called the connectivity number, c(G), of the graph.
Discrete Math for CS
Connectivity Number Algorithm:
G = (V,E)
begin V' := V; c := 0; while V' != do begin Choose y ∈ V'; Find all vertices joined to y by some path; Remove these vertices and y from V' and the corresponding edges of G; increment c; endend
Discrete Math for CS
Exercise
Use this algorithm to find the connectivity number of:
1
2
3
4
5
6
7
8
V' c{1,2,3,4,5,6,7,8} 0
Discrete Math for CS
Hamiltonian Graphs
A cycle that includes each vertex of a graph is called a Hamiltonian Cycle.
A graph with a Hamiltonian Cycle is called a Hamiltonian Graph.
Examples abound in telecommunications, railway timetables, etc.
There is no easy test for whether a graph is Hamiltonian.
Discrete Math for CS
Examples:
Suppose each vertex of a graph is adjacent to every other vertex. This is called a complete graph, K
n.
Complete graphs are Hamiltonian.
a
b
c
de
Discrete Math for CS
How Many Hamiltonian Cycles in K5?
Start at vertex a, there are 4 other vertices to choose from since all are adjacent to a.
From the second vertex there are three remaining vertices to choose from and from the third, two remaining.
From the last vertex go back to a.
Thus 4 x 3 x 2 = 24 paths from a to a but since all paths are bidirectional, we divide this by 2 to get 12 distinct Hamiltonian cycles starting at a.
Whether we count a-b-c-d-e-a and b-c-d-e-a-b as the same cycle depends on the application.
Discrete Math for CS
Exercise:
Show the graph below is not Hamiltonian.
a
b
c
de
f
i
gj
h
Vertices are of degree 2 or 3. If a Hamiltonian Cycle exists it includes both edges of the vertices of degree 2 so ab, bc, cd, gh, hi and ij allbelong to C. To include vertex f we must use fj and fg; fb can't be used.Likewise, je and gd can't be used so we must include ed. We have used all the vertices but have two disconnected graphs, not a Hamiltonian Cycle.
Discrete Math for CS
Traveling Salesperson Problem:
A sales person wishes to visit a number of town joined by roads. Find a route visiting each town only once and keeping travel costs to a minimum. The sales person must come back to the starting town at night.
Find the lowest cost Hamiltonian Cycle in a weighted graph.
Vertices are towns, edges are roads, weight is travel cost.
No none efficient algorithms that solve this problem in general.
For complex graphs the number of Hamiltonian cycles to consider is prohibitively large.
Good algorithms exist for sub-optimal solutions.
A sub-optimal solution is one that is better than “most”.
Discrete Math for CS
Nearest Neighbour Algorithm:
G = (V,E,W).
begin Choose v ∈ V; route := v; w := 0; v' := v; Mark v'; while (unmarked vertices remain) do begin Choose an unmarked vertex, u, closest to to v'; route := route u; w := w + weight of edge v'u; v' := u; Mark v'; end route := route v; w := w + weight of edge v'v;end
Discrete Math for CS
Example:
Apply Nearest Neighbour to:
a b
dc
6
5
8 7
3
10
u route w v'Initially - d 0 d
Discrete Math for CS
Trees:
A graph G = (V,E) is a tree if it is connected and acyclic.
The following are equivalent:
G is a tree with n vertices and m edges there is exactly one path between any two vertices of G G is connected and m = n – 1 G is connected and removing any edge disconnects the
graph G is acyclic and adding a new edge creates a cycle
Discrete Math for CS
Exercise:
Prove by induction on the number of vertices that if T is tree with n vertices and m edges then m = n - 1
Proof: True for n = 1 since the tree can have no edges in that case.
Suppose that the statement is true for any tree with fewer than n vertices.
Let T be a tree with n vertices. Remove an edge from T. This creates a disconnected graph with two connected components, neither with a cycle so both are trees. Since both trees are non-empty they have fewerthan n vertices so have l and k vertices respectively where n = l + k.
By induction they have l – 1 and k – 1 edges.
Hence the number of edges of T are (l – 1) + (k – 1) + 1 = l – k – 1 = n – 1.
Discrete Math for CS
Spanning Tree:
A subgraph of a connected graph that is a tree and contains all vertices of the original graph is called a spanning tree.
To construct a spanning tree start with any edge and keep adding edges as long as adding the edge does not create a cycle.
When no more edges can be added without creating an edge you have a spanning tree.
From the previous exercise we know how many edges are in this tree; specifically n – 1 where n is the number of vertices in the original graph.
Discrete Math for CS
Exercise:
Find two different spanning trees of
a b
c
de
f
g
Discrete Math for CS
Minimum Connector Problem:
A railroad network connects a number of towns is to be constructed, minimizing the miles of track. We assume the cost of a mile of track is fixed.
As a graph theoretical problem this is called the minimum spanning tree problem.
An efficient algorithm exists similar to Prim's Algorithm in Chapter 1.
Discrete Math for CS
Minimum Spanning Tree Algorithm:
G = (V,E) is a connected graph with weighted edges. The algorithm constructs a minimum spanning tree of G by successively selecting edges of least possible weight to construct a spanning tree. The minimum spanning tree is stored in T.
begin e := an edge of G of smallest weight; T := {e}; E' = E – {e}; while E' ∅∪–
Discrete Math for CS
Exercise:
Suppose A, B, C, D and E are five towns and the distance between them is given by the following chart
A B C D EA - 13 3 9 9B 13 - 11 11 13C 3 11 - 9 7D 9 11 9 - 2E 9 13 7 2 -
Discrete Math for CS
Solution:
Edges are chosen as follows:
choose DE, the edge of weight 2 next chose AC, an edge of weight 3. then choose CE, an edge of weight 7.
A
C
ED
Discrete Math for CS
Solution:
At this point the least weighted edges are AD, AE and CD.
All three create a cycle so these are excluded from E'.
A
C
ED
Discrete Math for CS
Solution:
The next least weighted edges are BC and BD.
Choosing either of these creates a spanning tree so there are two minimum spanning trees {AC, BC, CE, DE} and {AC, BD, CE, DE}; each with a total weight of 23.
A
C
ED
B
Discrete Math for CS
More Trees:
Trees are used to represent natural hierarchies, recursive “parent-child” relationships.
A tree is called rooted if one of its vertices is distinguished.
In a rooted tree nodes are to be found at different distances (path lengths) from the root. Recall there is only every one path between any two nodes of a tree.
A node at distance n from the root are said to be the child of the node on the path back to the root and at distance n-1 from the root. This node is called the parent.
rootb
a
parent of b
child of a
Discrete Math for CS
Family Trees:
Bernoulli Family Tree
Nicolausb. 1623
Nicolaus Ib. 1662
Jacques Ib. 1654
Jean Ib. 1667
Nicolaus IIb. 1687
Nicolaus IIIb. 1695
Daniel Ib. 1700
Jean IIb. 1710
Discrete Math for CS
How We View Trees:
Because of family trees we tend to view trees with the root at the top of the page and children under their parents.
Nodes at the same distance from the root are put at the same level on the page and given a level number.
The level number of the root is 0.
Discrete Math for CS
Rooted Tree Definition:
A single vertex is a tree.
If T1, T
2, ..., T
k are distinct trees with roots v
1, v
2, ..., v
k
respectively, the graph formed by attaching a new vertex, v, by a single edge to v
1, v
2, ..., v
k forms a tree T with root v.
The vertices v1, v
2, ..., v
k are the children of v.
T1
v1
T2
v2
Tk
vk
. . .
v
Discrete Math for CS
Subtrees:
Each vertex in a tree forms a subtree consisting of that vertex and all other nodes in the tree at greater distance from the original root and whose paths to the root contain the given vertex.
rootb
a
Example: a is the root of a tree consisting of the two vertices a and band the edge between them.
Discrete Math for CS
Roots and Leaves:
Definition of a wild pig.
The leaves of a rooted tree are the vertices with no children.
Vertices of a rooted tree that are not leaves are called internal vertices.
In CS, we have studied in detain binary rooted trees, rooted trees where each vertex has at most 2 children.
In a binary rooted tree, the two subtrees of any given vertex are referred as the left-subtree and right-subtree of that vertex.
If a vertex has no left-subtree we say its left-subtree is null.
Discrete Math for CS
Example A
B
D
G H
E
C
F
I J
Exercises: Describe: the root of T the root of left-subtree of B the leaves of T the children of vertex C
Discrete Math for CS
Exercise:
A binary tree is a tree where each parent vertice has at most 2 child vertices.
A complete binary tree is a binary tree where a vertex has either 0 or 2 children.
Prove by induction that a complete binary tree if of depth n has 2n leaves (vertices with no children) n >= 1.
n == 1: The tree has 3 vertices – root and two children. 2 == 21 so true.
Assume true for all complete binary trees of depth n-1 and suppose T is a complete binary tree of depth n. Removing all leaf vertices from T producesa complete binary tree of depth n-1. By induction this has 2n-1 leaf vertices.Since each of these has two children in T and all these children are leaf vertices in T the number of leaf vertices in T is 2n-1*2 = 2n.
Discrete Math for CS
Sorting and Searching:
Binary trees are good structures for holding data that needs to be searched or sorted. The tree structure makes it easy to ask and answer questions.
Lists of numbers or strings in lexicographical order are good things to store in binary trees.
Basic Principle of the Binary Search Tree: Each item in the left sub-tree of any vertex is less than the data stored at the vertex in question and each item in the right sub-tree is greater than the same data item.
Discrete Math for CS
Exercise:
Store the words in “my computer has a chip on its shoulder” in a BST.
my
computer on
a has shoulder
chip its
Discrete Math for CS
Motivation:
Efficient search algorithms are easy to devise for data stored in a binary search tree.
Example of data structure complexity vs algorithm complexity tradeoff.
Discrete Math for CS
Example:
Hold student records, arranged alphabetically by student name, in a BST for insert, search and print.
Our algorithms will be recursive.
Algorithms based upon a key. The key will be student name.
Discrete Math for CS
Search:
search(tree)begin if ( tree is null ) then search := false; else if (search key == root key) then search := true; else if ( search key < root key ) then search := search(left sub-tree); else search := search(right sub-tree);end
K
C T
M V
Search for R.
Discrete Math for CS
Insert:
We create a new vertex to the right or left of an existing vertex.
insert(item,tree)begin if ( tree is null ) then add new item; else if (search key == root key) then print “item already present”; else if ( search key < root key ) then search := insert( item, left sub-tree); else search := insert( item, left sub-tree);end
K
C T
M V
Insert items R, A and L
Discrete Math for CS
Print:
Requires a traversal of the entire tree.
in-order traversal(tree)begin if ( tree is null) then do nothing; else begin in-order traversal (left sub-tree); print root key; in-order traversal (right sub-tree); endend
K
C T
M VA
L R