graphs discrete structure cs203. adjacency matrix we already saw a way of representing relations on...

46
Graphs Discrete Structure CS203

Upload: diane-bryan

Post on 14-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Graphs

Discrete Structure CS203

Page 2: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Adjacency Matrix

We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) MR

1 12 23 34 4

1

2

3

4

1000

1100

1110

1111

Page 3: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

MatricesMatrices

10000006

01010105

11100004

00101003

00011012

00000111

6,45,44,35,23,25,12,1

0010006

0010115

1101004

0010103

0101012

0100101

654321

Page 4: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Adjacency Matrix-Directed Multigraphs

Can easily generalize to directed multigraphs by putting in the number of edges between vertices, instead of only allowing 0 and 1:

For a directed multigraph G = (V,E ) define the matrix AG by:

Rows, Columns –one for each vertex in VValue at i th row and j th column is The number of edges with source the i th vertex

and target the j th vertex

Page 5: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Adjacency Matrix Representation for a Directed Graph

1 2 3 4 5

12345

0 1 0 0 10 0 1 1 10 0 0 1 00 0 0 0 10 0 0 0 0

1

5

2

43

Page 6: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Adjacency Matrix-Directed Multigraphs

A:

1

2

3

0000

0210

0210

1030

4

Page 7: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Adjacency Matrix-General

A:

Notice that answer is symmetric.

1 2

3 4

1000

0011

0122

0120

Page 8: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Counting PathsLet A be the Adjacency Matrix. What is A2?

d

c

b

e

a

Page 9: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Adjacency-list representation for a directed graph.

1

5

122

5

4 4

3 3

2 5

5 3 4

4

5

5

Variation: Can keep a second list of edges coming into a vertex.

Page 10: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

10

Paths

Q: Find a longest possible simple path in the following graph:

1 2

3 4

e1

e3

e2

e4e5

e6

e7

Page 11: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Paths in Directed Graphs

Q: Consider digraph adjacency matrix:

1. Find a path from 1 to 4.2. Is there a path from 4 to 1?

0010

0011

1010

0101

Page 12: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Euler Paths and Circuits

An Euler path in a graph G is a simple path containing every edge in G. An Euler circuit (or Euler cycle) is a cycle which is an Euler path.

NOTE: The definition applies both to undirected as well as directed graphs of all types.

NOTE: the graph is Euler circuit ten every node is even degree

Page 13: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Hamilton Paths and Circuits

A Hamilton path in a graph G is a path which visits ever vertex in G exactly once. A Hamilton circuit (or Hamilton cycle) is a cycle which visits every vertex exactly once, except for the first vertex, which is also visited at the end of the cycle.

NOTE: Again, the definition applies both to undirected as well as directed graphs of all types.

Page 14: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Hamilton Paths and Cycles

a path or cycle that contain every vertex

Unlike Euler circuit, there is no knownnecessary and sufficient condition for a graph to be Hamiltonian.

a b c

d e f

g hi

There is a Hamilton path, but no Hamilton cycle.

Page 15: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

15

Shortest Path ProblemsWe can assign weights to the edges of

graphs, for example to represent the distance between cities in a railway network:

ChicagoChicago

TorontoToronto

New YorkNew York

BostonBoston

600600

700700

200200

650650

Page 16: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Shortest Path Problems

Such weighted graphs can also be used to model computer networks with response times or costs as weights.

What is the shortest path between two vertices in the graph, that is, the path with the minimal sum of weights along the way?

This corresponds to the shortest train connection or the fastest connection in a computer network.

Page 17: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Dijkstra’s Algorithm

procedure Dijkstra(G: weighted connected simple graph with vertices a = v0, v1, …, vn = z and positive weights w(vi, vj), where w(vi, vj) = if {vi, vj} is not an edge in G)

for i := 1 to nL(vi) :=

L(a) := 0S := {the labels are now initialized so that the label of

a is zero and all other labels are , and the distinguished set of vertices S is empty}

Page 18: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Dijkstra’s Algorithm

while zSbegin

u := the vertex not in S with minimal L(u)S := S{u}for all vertices v not in Sif L(u) + w(u, v) < L(v) then L(v) := L(u) +

w(u, v)

{this adds a vertex to S with minimal label and updates the labels of vertices not in S}

end {L(z) = length of shortest path from a to z}

Page 19: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Dijkstra’s AlgorithmExample:

aa

bb dd

zz

eecc

44

22

11

55

88

1010

22

66

3300

Step 0Step 0

Page 20: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Dijkstra’s AlgorithmExample:

aa

bb dd

zz

eecc

44

22

11

55

88

1010

22

66

3300

Step 1Step 1

4 (a)4 (a)

2 (a)2 (a)

Page 21: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Dijkstra’s AlgorithmExample:

aa

bb dd

zz

eecc

44

22

11

55

88

1010

22

66

3300

Step 2Step 2

4 (a)4 (a)

2 (a)2 (a)

3 (a, c)3 (a, c) 10 (a, c)10 (a, c)

12 (a, c)12 (a, c)

Page 22: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Dijkstra’s AlgorithmExample:

aa

bb dd

zz

eecc

44

22

11

55

88

1010

22

66

3300

Step 3Step 3

4 (a)4 (a)

2 (a)2 (a)

3 (a, c)3 (a, c) 10 (a, c)10 (a, c)

12 (a, c)12 (a, c)

8 (a, c, b)8 (a, c, b)

Page 23: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Dijkstra’s AlgorithmExample:

aa

bb dd

zz

eecc

44

22

11

55

88

1010

22

66

3300

Step 4Step 4

4 (a)4 (a)

2 (a)2 (a)

3 (a, c)3 (a, c) 10 (a, c)10 (a, c)

12 (a, c)12 (a, c)

8 (a, c, b)8 (a, c, b)

10 (a, c, b, d)10 (a, c, b, d)

14 (a, c, b, d)14 (a, c, b, d)

Page 24: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Dijkstra’s AlgorithmExample:

aa

bb dd

zz

eecc

44

22

11

55

88

1010

22

66

3300

Step 5Step 5

4 (a)4 (a)

2 (a)2 (a)

3 (a, c)3 (a, c) 10 (a, c)10 (a, c)

12 (a, c)12 (a, c)

8 (a, c, b)8 (a, c, b)

10 (a, c, b, d)10 (a, c, b, d)

14 (a, c, b, d)14 (a, c, b, d)13 (a, c, b, d, e)13 (a, c, b, d, e)

Page 25: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Dijkstra’s AlgorithmExample:

aa

bb dd

zz

eecc

44

22

11

55

88

1010

22

66

3300

Step 6Step 6

4 (a)4 (a)

2 (a)2 (a)

3 (a, c)3 (a, c) 10 (a, c)10 (a, c)

12 (a, c)12 (a, c)

8 (a, c, b)8 (a, c, b)

10 (a, c, b, d)10 (a, c, b, d)

14 (a, c, b, d)14 (a, c, b, d)13 (a, c, b, d, e)13 (a, c, b, d, e)

Page 26: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

The Traveling Salesman Problem

The traveling salesman problem is one of the classical problems in computer science.

A traveling salesman wants to visit a number of cities and then return to his starting point. Of course he wants to save time and energy, so he wants to determine the shortest path for his trip.

The problem then is to find the circuit of minimum total weight that visits each vertex exactly once.

Page 27: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

The Traveling Salesman ProblemExample: What path would the traveling

salesman take to visit the following cities?

ChicagoChicago

TorontoToronto

New YorkNew York

BostonBoston

600600

700700

200200

650650 550550700700

Solution:Solution: The shortest path is Boston, New York, Chicago, The shortest path is Boston, New York, Chicago, Toronto, Boston (2,000 miles).Toronto, Boston (2,000 miles).

Page 28: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

28

Trees

Definition: A tree is a connected undirected graph with no simple circuits.

Since a tree cannot have a simple circuit, a tree cannot contain multiple edges or loops.

Therefore, any tree must be a simple graph.

Theorem: An undirected graph is a tree if and only if there is a unique simple path between any of its vertices.

Page 29: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

29

TreesExample: Are the following graphs trees?

No.No.

Yes.Yes.

Yes.Yes.

No.No.

Page 30: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Tree Terminology

If v is a vertex in a rooted tree other than the root, the parent of v is the unique vertex u such that there is a directed edge from u to v.

When u is the parent of v, v is called the child of u.

Vertices with the same parent are called siblings.

The ancestors of a vertex other than the root are the vertices in the path from the root to this vertex, excluding the vertex itself and including the root.

Page 31: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Tree Terminology

The descendants of a vertex v are those vertices that have v as an ancestor.

A vertex of a tree is called a leaf if it has no children.

Vertices that have children are called internal vertices.

If a is a vertex in a tree, then the subtree with a as its root is the subgraph of the tree consisting of a and its descendants and all edges incident to these descendants.

Page 32: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

TreesExample I: Family tree

JamesJames

ChristineChristine BobBob

FrankFrank JoyceJoyce PetraPetra

Page 33: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

TreesExample III: Arithmetic expressions

++ --

yy zz xx yy

This tree represents the expression (y + z)(x - y).This tree represents the expression (y + z)(x - y).

Page 34: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Binary Search Trees

If we want to perform a large number of searches in a particular list of items, it can be worthwhile to arrange these items in a binary search tree to facilitate the subsequent searches.

A binary search tree is a binary tree in which each child of a vertex is designated as a right or left child, and each vertex is labeled with a key, which is one of the items.

When we construct the tree, vertices are assigned keys so that the key of a vertex is both larger than the keys of all vertices in its left subtree and smaller than the keys of all vertices in its right subtree.

Page 35: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Spanning Trees

Definition: Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G.

Note: A spanning tree of G = (V, E) is a connected graph on V with a minimum number of edges (|V| - 1).

Example: Since winters in Boston can be very cold, six universities in the Boston area decide to build a tunnel system that connects their libraries.

Page 36: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Spanning TreesThe complete graph including all possible tunnels:

BrandeisBrandeis HarvardHarvard

MITMIT

TuftsTuftsBUBU

UMassUMass

The spanning trees of this graph connect all libraries with a The spanning trees of this graph connect all libraries with a minimum number of tunnels. minimum number of tunnels.

Page 37: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Spanning TreesExample for a spanning tree:

BrandeisBrandeis HarvardHarvard

MITMIT

TuftsTuftsBUBU

UMassUMass

Since there are 6 libraries, 5 tunnels are sufficient to connect all Since there are 6 libraries, 5 tunnels are sufficient to connect all of them. of them.

Page 38: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Coloring graph

To be reported

Page 39: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Application to Ad Hoc Networking

Networks can be represented by graphsThe mobile nodes are verticesThe communication links are edges

• Routing protocols often use shortest path algorithms

• This lecture is background material to the routing algorithms

Vertices

Edges

Page 40: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Graphs ↔ NetworksGraph

(Network)Vertexes(Nodes)

Edges(Arcs)

Flow

Communications

Telephones exchanges, computers, satellites

Cables, fiber optics, microwave relays

Voice, video, packets

Circuits Gates, registers, processors

Wires Current

Mechanical JointsRods, beams, springs

Heat, energy

Hydraulic Reservoirs, pumping stations, lakes

Pipelines Fluid, oil

Financial Stocks, currency Transactions Money

Transportation Airports, rail yards, street intersections

Highways, railbeds, airway routes

Freight, vehicles, passengers

Page 41: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Network data sets

Collaboration graphs Wikipedia, World of Warcraft, Citation graphs

Who-talks-to-whom graphs Microsoft IM, Cell phone graphs

Information linkage Hyperlinks

Technological networks Power grids, communication links, Internet

Natural and biological networks Food webs, neural interconnections, cell

metabolism

Page 42: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

RecapA graph consists of nodes and edgesGraphs can be directed or undirected, weighted, signed or unweighted Paths between nodes (simple vs. non-

simple) Cycles Connectivity Components (and the giant component) Distance (and BFS)Six degrees of separation can be checked with BFS

Page 43: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Distance

Def: The distance between a pair of nodes is the edge length of the shortest path between them Just number of edges. Can be thought

of as all edges having weight of 1

What‘s the distance between MIT and SDC?

Page 44: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Distance: Breadth-first search (BFS)

Page 45: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

Link Failure

A

B

EC

D

G

F

Simple rerouting case

• F detects that link to G has failed

• F sets a distance of ∞ to G and sends update to A

• A sets a distance of ∞ to G since it uses F to reach G

• A receives periodic update from C with 2-hop path to G (via D)

• A sets distance to G to 3 and sends update to F

• F decides it can reach G in 4 hops via A

Page 46: Graphs Discrete Structure CS203. Adjacency Matrix We already saw a way of representing relations on a set with a Boolean matrix: R digraph(R) M R1234

• Link from A to E fails• A advertises distance of ∞ to E• B and C had advertised a

distance of 2 to E (prior to the link failure)

• Upon reception of A’s routing update B decides it can reach E in 3 hops; and advertises this to A

• A decides it can read E in 4 hops; advertises this to C

• C decides that it can reach E in 5 hops…

Link Failure

A

B

EC

D

G

F

Routing loop case

This behavior is called count-to-infinity