week 11 - wednesday. what did we talk about last time? graphs euler paths and tours

38
CS322 Week 11 - Wednesday

Upload: hilary-stanley

Post on 01-Jan-2016

236 views

Category:

Documents


1 download

TRANSCRIPT

CS322Week 11 - Wednesday

Last time

What did we talk about last time? Graphs Euler paths and tours

Questions?

Logical warmup

One hundred ants are walking along a meter long stick

Each ant is traveling either to the left or the right with a constant speed of 1 meter per minute

When two ants meet, they bounce off each other and reverse direction

When an ant reaches an end of the stick, it falls off

Will all the ants fall off? What is the longest amount of time that you

would need to wait to guarantee that all ants have fallen off?

Back to Graphs

Hamiltonian circuits

An Euler circuit has to visit every edge of a graph exactly once A Hamiltonian circuit must visit every vertex of a graph exactly

once (except for the first and the last) If a graph G has a Hamiltonian circuit, then G has a subgraph H with

the following properties: H contains every vertex of G H is connected H has the same number of edges as vertices Every vertex of H has degree 2

In some cases, you can use these properties to show that a graph does not have a Hamiltonian circuit

In general, showing that a graph has or does not have a Hamiltonian circuit is NP-complete (widely believed to take exponential time)

Does the following graph have a Hamiltonian circuit?

eb

a c

d

Matrix Representations of Graphs

Matrices

As you presumably know, a matrix is a rectangular array of elements

An m x n matrix has m rows and n columns

𝐴=[𝑎11 𝑎12 ⋯𝑎21 𝑎22 ⋯⋮ ⋮ ¿

𝑎1 𝑗 ⋯ 𝑎1𝑛𝑎2 𝑗 ⋯ 𝑎2𝑛⋮ ¿ ¿

⋯¿𝑎𝑖𝑗 ⋯ 𝑎𝑖𝑛⋮ ⋯ ⋮𝑎𝑚𝑗 ⋯ 𝑎𝑚𝑛

¿]

Graph representations

There are many, many different ways to represent a graph

If you get tired of drawing pictures or listing ordered pairs, a matrix is not a bad way

To represent a graph as an adjacency matrix, make an n x n matrix, where n is the number of vertices

Let the nonnegative integer at aij give the number of edges from vertex i to vertex j

A simple graph will always have either 1 or 0 for every location

Graph to matrix examples What is the adjacency matrix for the following graph?

What about for this one?

v1

v3

v2

v3

v2

v1

Matrix to graph example

Draw a graph corresponding to this matrix

0012

1100

2011

0110

A

Another graph to matrix

What's the adjacency matrix of this graph?

Note that the matrix is symmetric In a symmetric matrix, aij = aji for all 1 ≤ i ≤ n

and 1 ≤ j ≤ n All undirected graphs have a symmetric matrix

representation

v1

v4

v2

v3

Matrix multiplications

To multiply matrices A and B, it must be the case that A is an m x k matrix and that B is a k x n matrix

Then, the ith row, jth column of the result is the dot product of the ith row of A with the jth column of B

In other words, we could compute element cij in the result matrix C as follows:

k

xxjix ba

1

Matrix multiplication practice

Multiply matrices A and B

12

22

34

011

302

B

A

A few points about matrix multiplication

Matrix multiplication is associative That is, A(BC) = (AB)C

Matrix multiplication is not commutative AB is not always equal to BA (for one thing, BA might not

even be legal if AB is) There is an n x n identity matrix I such that, for any m x n matrix A, AI = A I is all zeroes, except for the diagonal (where row =

column) which is all ones We can raise square matrices to powers using the

following recursive definition A0 = I, where I is the n x n identity matrix Ak = AAk-1, for all integers k ≥ 1

Finding powers of a matrix

Is A symmetric? Compute A0

Compute A1

Compute A2

Compute A3

02

21A

Matrix powers for graphs We can find the number of walks of length k that

connect two vertices in a graph by raising the adjacency matrix of the graph to the kth power

Raising a matrix to the zeroth power means you can only get from a vertex to itself (identity matrix)

Raising a matrix to the first power means that the number of paths of length one from one vertex to another is exactly the number of edges between them

The result holds for all k, but we aren't going to prove it

Graph Isomorphism

Isomorphism invariants

A property is called an isomorphism invariant if its truth or falsehood does not change when considering a different (but isomorphic) graph

10 common isomorphism invariants:1. Has n vertices2. Has m edges3. Has a vertex of degree k4. Has m vertices of degree k5. Has a circuit of length k6. Has a simple circuit of length k7. Has m simple circuits of length k8. Is connected9. Has an Euler circuit10. Has a Hamiltonian circuit

Using invariants to disprove isomorphism

If any of the invariants have different values for two different graphs, those graphs are not isomorphic

Use the 10 invariants given to show that the following pair of graphs is not isomorphic

TreesWhat are they and how are they useful?Student Lecture

Trees

Trees

A tree is a graph that is circuit-free and connected

Examples:

A graph made up of disconnected

trees is called a forest

Applications of trees

Trees have almost unlimited applications

You should all be familiar with the concept of a decision tree from programming

Score on Part I

Score on Part II

Score on Part II

Math 120

Math 110

Math 110

Math 100

Math 100

< 8

= 8, 9, 10

> 10

> 10

10> 6

6

Parse trees

A grammar for a formal language (such as we will discuss next week or the week after) is made up of rules that allow non-terminals to be turned into other non-terminals or terminals

For example:1. <sentence> <noun phrase><verb phrase>2. <noun phrase> <article><noun> |

<article><adjective><noun>3. <verb phrase> <verb><noun phrase>4. <article> a | an | the5. <adjective> funky6. <noun> DJ | beat7. <verb> plays | spins

Make a parse tree corresponding to the sentence, "The DJ plays a funky beat"

Describing trees

Any tree with more than one vertex has at least one vertex of degree 1

If a vertex in a tree has degree 1 it is called a terminal vertex (or leaf)

All vertices of degree greater than 1 in a tree are called internal vertices (or branch vertices)

A property of trees

For any positive integer n, a tree with n vertices must have n – 1 edges

Prove this by mathematical inductionHint: Any tree with 2 or more nodes

has a vertex of degree 1. What happens when you remove that vertex?

Rooted trees

In a rooted tree, one vertex is distinguished and called the root

The level of a vertex is the number of edges along the unique path between it and the root

The height of a rooted tree is the maximum level of any vertex of the tree

The children of any vertex v in a rooted tree are all those nodes that are adjacent to v and one level further away from the root than v

Two distinct vertices that are children of the same parent are called siblings

If w is a child of v, then v is the parent of w If v is on the unique path from w to the root, then v is

an ancestor of w and w is a descendant of v

Rooted tree example

Consider the following tree, rooted at 0

What is the level of 5? What is the level of 0? What is the height of this tree? What are the children of 3? What is the parent of 2? What are the siblings of 8? What are the descendants of 3?

3

0

5

21

7 9

6

4

8

Binary trees

A binary tree is a rooted tree in which every parent has at most two children

Each child is designated either the left child or the right child

In a full binary tree, each parent has exactly two children

Given a parent v in a binary tree, the left subtree of v is the binary tree whose root is the left child of v

Ditto for right subtree

Binary tree applications

As we all know from data structures, a binary tree can be used to make a data structure that is efficient for insertions, deletions, and searches

But, it doesn't stop there! We can represent binary arithmetic with a

binary tree Make a binary tree for the expression ((a – b)∙c) + (d/e) The root of each subtree is an operator Each subtree is either a single operand or another

expression

Full Binary Tree Theorem 1

If k is a positive integer and T is a full binary tree with k internal vertices, then T has a total 2k + 1 vertices and has k + 1 terminal vertices

Prove it!Hint: Induction isn't needed. We

just need to relate the number of non-terminal nodes to the number of terminal nodes

Full Binary Tree Theorem 2

If T is a full binary tree with height h, then it has 2h+1 – 1 vertices

Prove it using induction!

Generalizing that result

If T is a binary tree with t terminal vertices and height h, then t 2h

Prove it using strong induction on the height of the tree

Hint: Consider cases where the root of the tree has 1 child and 2 children separately

Quiz

Upcoming

Next time…

Spanning trees Graphing functions

Reminders

Keep reading Chapter 10 Start Chapter 11 Start work on Assignment 9

Due next Friday