cs322

39
CS322 Week 10 - Thursday

Upload: eileen

Post on 06-Jan-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Week 11 - Friday. CS322. Last time. What did we talk about last time? Combinations. Questions?. Logical warmup. A butcher goes to the market with $100 He has to buy exactly 100 animals (for no especially good reason) There are cows, geese and chicken for sale Cows are $15 each - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS322

CS322Week 10 - Thursday

Page 2: CS322

Last time

What did we talk about last time? Probability axioms Conditional probability Bayes' Theorem

Page 3: CS322

Questions?

Page 4: CS322

Logical warmup

A butcher goes to the market with $100 He has to buy exactly 100 animals (for no

especially good reason) There are cows, geese and chicken for sale

Cows are $15 each Geese are $1 each Chickens are $0.25 each

He has to buy at least one of each animal and has to spend all his money

What does the butcher buy?

Page 5: CS322

PHIFE DAWG • 1970 - 2016

Page 6: CS322

GraphsStudent Lecture

Page 7: CS322

Graphs

Page 8: CS322

Graphs

A graph G is made up of two finite sets Vertices: V(G) Edges: E(G)

Each edge is connected to either one or two vertices called its endpoints

An edge with a single endpoint is called a loop Two edges with the same sets of endpoints are

called parallel Edges are said to connect their endpoints Two vertices that share an edge are said to be

adjacent A graph with no edges is called empty

Page 9: CS322

The purpose of graphs

Graphs can be used to represent connections between arbitrary things Streets connecting towns Links connecting computers in a network Friendships between people Enmities between people Almost anything…

Page 10: CS322

Graph representation

We can represent graphs in many ways

One is simply by listing all the vertices, all the edges, and all the vertices connected by each edge

Let V(G) = {v1, v2, v3, v4, v5, v6} Let E(G) = {e1, e2, e3, e4, e5, e6, e7} Edges connect the following

vertices: Draw the graph with the given

connections

Edge Vertices

e1

{v1, v2}

e2

{v1, v3}

e3

{v1, v3}

e4

{v2, v3}

e5

{v5, v6}

e6 {v5}

e7 {v6}

Page 11: CS322

Drawing graphs

Graphs can (generally) be drawn in many different ways

We can label graphs to show that they are the same

Label these two graphs to show they are the same:

Page 12: CS322

Special graphs

A simple graph does not have any loops or parallel edges Let n be a positive integer A complete graph on n vertices, written Kn, is a simple graph

with n vertices such that every pair of vertices is connected by an edge

Draw K1, K2, K3, K4, K5

A complete bipartite graph on (m, n) vertices, written Km,n is a simple graph with a set of m vertices and a disjoint set of n vertices such that: There is an edge from each of the m vertices to each of the n vertices There are no edges among the set of m vertices There are no edges among the set of n vertices

Draw K3,2 and K3,3

A subgraph is a graph whose vertices and edges are a subset of another graph

Page 13: CS322

Degree

The degree of a vertex is the number of edges that are incident on the vertex

The total degree of a graph G is the sum of the degrees of all of its vertices

What's the relationship between the degree of a graph and the number of edges it has?

What's the degree of a complete graph with n vertices?

Note that the number of vertices with odd degree must be even… why?

Page 14: CS322

Paths and Circuits

Page 15: CS322

Königsberg

Used to be Königsberg, Prussia Now called Kaliningrad, Russia On the Pregel River, including two

large islands

Page 16: CS322

Seven Bridges of Königsberg

In 1736, the islands were connected by seven bridges

In modern times, there are only five

Page 17: CS322

The Challenge

After a lazy Sunday and a bit of drinking, the citizens would challenge each other to walk around the city and try to find a path which crossed each bridge exactly once

Page 18: CS322

Euler’s Solution

What did Euler find? The same thing you did: nothing But, he also proved it was impossible Here’s how:

Center Island

North Shore

East Island

South Shore

Page 19: CS322

Graph Theoretical View

By simplifying the problem into a graph, the important features are clear

To arrive as many times as you leave, the degrees of each node must be even (except for the starting and ending points)

Center Island

North Shore

East Island

South Shore

Page 20: CS322

Definitions

A walk from v to w is a finite alternating sequence of adjacent vertices and edges of G, starting at vertex v and ending at vertex w A walk must begin and end at a vertex

A path from v to w is a walk that does not contain a repeated edge

A simple path from v to w is a path that does not contain a repeated vertex

A closed walk is a walk that starts and ends at the same vertex

A circuit is a closed walk that does not contain a repeated edge

A simple circuit is a circuit that does not have a repeated vertex other than the first and last

Page 21: CS322

Notation

We can always pin down a walk unambiguously if we list each vertex and each edge traversed

How would we notate a walk that starts at v1 and ends at v2 and visits every edge exactly once in the following graph?

However, if a graph has no parallel edges, then a sequence of vertices uniquely determines the walk

v1

v2

v3

e1

e2

e3

e4

Page 22: CS322

Connectedness

Vertices v and w of G are connected iff there is a walk from v to w

Graph G is connected iff all pairs of vertices v and w are connected to each other

A graph H is a connected component of a graph G iff H is a subgraph of G H is connected No connected subgraph of G has H as a subgraph and

contains vertices or edges that are not in H A connected component is essentially a connected

subgraph that cannot be any larger Every (non-empty) graph can be partitioned into one or

more connected components

Page 23: CS322

Euler circuits

What if you want to find an Euler circuit of your own?

If a graph is connected, non-empty, and every node in the graph has even degree, the graph has an Euler circuit

Algorithm to find one:1. Pick an arbitrary starting vertex2. Move to an adjacent vertex and remove the edge

you cross from the graph▪ Whenever you choose such a vertex, pick an edge that

will not disconnected the graph

3. If there are still uncrossed edges, go back to Step 2

Page 24: CS322

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

Page 25: CS322

Matrix Representations of Graphs

Page 26: CS322

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 𝑎1 2 ⋯𝑎21 𝑎22 ⋯⋮ ⋮ ¿

𝑎1 𝑗 ⋯ 𝑎1𝑛

𝑎2 𝑗 ⋯ 𝑎2𝑛

⋮ ¿ ¿⋯¿

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

¿]

Page 27: CS322

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

Page 28: CS322

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

What about for this one?

v1

v3

v2

v3

v2

v1

Page 29: CS322

Matrix to graph example

Draw a graph corresponding to this matrix

0012

1100

2011

0110

A

Page 30: CS322

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

Page 31: CS322

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

Page 32: CS322

Matrix multiplication practice

Multiply matrices A and B

12

22

34

011

302

B

A

Page 33: CS322

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

Page 34: CS322

Finding powers of a matrix

Is A symmetric? Compute A0

Compute A1

Compute A2

Compute A3

02

21A

Page 35: CS322

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

Page 36: CS322

Quiz

Page 37: CS322

Upcoming

Page 38: CS322

Next time…

Directed graphs Graph isomorphism Trees

Page 39: CS322

Reminders

Work on Homework 8 Due next Friday

Keep reading Chapter 10No class on Friday or Monday!