discrete structures cisc 2315 fall 2010 graphs & trees

38
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Upload: maximilian-lewis

Post on 08-Jan-2018

230 views

Category:

Documents


0 download

DESCRIPTION

Components of a Graph edge vertex (node)

TRANSCRIPT

Page 1: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Discrete StructuresCISC 2315FALL 2010

Graphs & Trees

Page 2: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Graphs

A graph is a discrete structure, with discrete components…

Page 3: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Components of a Graph

edge

vertex(node)

Page 4: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Vertices

A graph G = (V, E), where V is the set of all the vertices in the graph, and E is the set of all edges in the graph.

The elements of V are typically named u or v.

Page 5: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Types of Edges

Undirected edges: In this case, there is a function f from E to

An edge is a loop if f(e) = {u, u} = {u} for some u in V.

},|},{{ Vvuvu

Note unordered

loop

Undirected graph

Page 6: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Types of Edges

Directed edges: In this case, there is a function f from E to

An edge is a loop if f(e) = (u, u) for some u in V.

Note ordered

loop

},|),{( Vvuvu

Directed graph

Page 7: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Graph Terminology

Two vertices u and v in an undirected graph G are adjacent (neighbors) in G if {u,v} is an edge of G. If e={u,v}, then the edge e is called incident with the vertices u and v. Edge e is said to connect u and v. Also, vertices u and v are called endpoints of the edge {u,v}.

Page 8: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Graph Terminology

u v

Vertices u and v are adjacent. Edge e is incident with u and v; e connects u and v.Vertices u and v are endpoints of e.

e

Page 9: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Degree

The degree of a vertex in an undirected graph is the number of edges incident with it. A loop contributes twice to the degree.

A vertex of degree zero is called isolated. A vertex of degree 1 is called pendant.

Page 10: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Graph Terminology

u ve

What is the deg(u)? What is the deg(v)?

Page 11: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Graph Terminology

In a directed graph G with edge (u,v), u is said to be adjacent to v and v is said to be adjacent from u. Vertex u is the initial vertex and v is the terminal (end) vertex of (u,v). The initial and terminal vertices of a loop are the same.

Page 12: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Graph Terminology

u v

Vertex u is adjacent to v and v is adjacentfrom u. Vertex u is the initial vertex and v is the terminal vertex of edge (u,v).

e

Page 13: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Degree

The in-degree of a vertex v, deg-(v) in a directed graph is the number of edges with v as their terminal vertex. The out-degree of v, deg+(v), is the number of edges with v as their initial vertex. A loop contributes 1 to both the in-degree and out-degree.

Page 14: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Graph Terminology

u v

What is the in-degree of u? the out-degree of u?What is the in-degree of v? the out-degree of v?

e

Page 15: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Simple Graphs

A graph is simple if it has only one edge connecting each pair of vertices.

Page 16: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Simple Graphs

simple

not simple

Page 17: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Bipartite Graph

A simple graph G=(V,E) is bipartite if V can be partitioned into disjoint sets V1 and V2 such that every edge in the graph connects a vertex in V1 and a vertex in V2. No edge in G connects either two vertices in V1 or two vertices in V2.

Page 18: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Bipartite Graph

Page 19: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Union of Graphs

The union of two simple graphs G1=(V1,E1) and G2=(V2,E2) is the simple graph ),( 212121 EEVVGG

Page 20: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Union of a Graphs

Page 21: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Matrices

Used to represent graphs in a computer program.A matrix is a rectangular array of numbers.

a11 a12 … a1n

a21 a22 … a2n

.

.

.a1m a2m … amn

A =

This is an m x n matrix.

Page 22: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Adjacent Vertices in a Graph

ga

b

c

d

e

f

a b,c,d,gb a,dc a,d,ed a,b,c,e,fe c,d,f,g

f ?g ?

vertexadjacentvertices

Page 23: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Adjacency Matrix to Represent a Graph

ga

b

c

d

e

f

a b c d e f g

a 0 1 1 1 0 0 1

b 1 0 0 1 0 0 0

c 1 0 0 1 1 0 0

d 1 1 1 0 1 1 0

e 0 0 1 1 0 1 1

f 0 0 0 1 0 0 1

g 1 0 0 0 1 1 0

Page 24: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Incidence to Represent a Graph

ga

b

c

d

e

f

e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11

a 1 1 1 0 0 0 1 0 0 0 0

b 0 1 0 1 0 0 0 0 0 0 0

c 0 0 0 0 1 1 1 0 0 0 0

d 0 0 1 1 1 0 0 1 0 0 0

e 0 0 0 0 0 1 0 0 1 1 0

f 0 0 0 0 0 0 0 1 1 0 1

g 1 0 0 0 0 0 0 0 0 1 1

e1

e2e3

e4

e5

e6

e8

e9

e10

e11

e7

Page 25: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Path

A path is a sequence of edges that begins with a vertex of the graph and travels along edges of the graph, always connecting pairs of adjacent vertices.

A path of length n from u to v in an undirected graph is a sequence of edges e1,…,en that begins with u and ends with v. The path is a circuit if u=v. The path passes through the vertices that are visited, and it traverses the edges on the path. A path or circuit is simple if it doesn’t contain the same edge more than once.

Page 26: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Path in Matrix an Undirected Graph

u v

Page 27: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Connectedness

An undirected graph is connected if there is a path between every pair of distinct vertices in the graph.

Page 28: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Is this graph connected?

Page 29: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Trees

Note that a tree is a connected undirected graph that has no simple circuits.

Page 30: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Path

A path of length n from u to v in a directed graph is a sequence of directed edges e1,…,en that begins with u and ends with v. The path is a circuit if u=v. The path passes through the vertices that are visited, and it traverses the edges on the path. A path or circuit is simple if it doesn’t contain the same edge more than once.

Page 31: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Circuit in a Directed Graph

u=v

Page 32: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Connectedness

A directed graph is strongly connected if there is a path from a to b and from b to a whenever a and b are vertices in the graph.

A directed graph is weakly connected if there is a path between every two vertices in the underlying undirected graph.

Page 33: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Is this graph strongly/weakly connected?

Page 34: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Example Applications of Graphs

Designing airplane routes Modeling the interconnections and

information flow in local and wide area computer networks

Models of ecologies Finding the shortest path between two

locations (uses distances along the edges) Solving search problems in artificial

intelligence

Page 35: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Lists - revisited

A list is a finite ordered sequence of zero or more elements that can be repeated.

The difference between lists and tuples is in what parts can be randomly accessed.

Head (L) and Tail (L) is Memory representation of a list in the computer

.,,),,,(,),,,( zyxzyxwtwzyxwh

b c

a

eL

cons (a,L)

d

head (L) = b tail (L)

Page 36: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Computer representation of a tree

a

b c d

e

a

b c d

e

.,,,, edcbaT

Page 37: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Binary Tree

aa

b c

d e

b c

d e

.,,,, caebdT

Binary trees can be used to represent sets whose elements have some ordering.Such a tree is called a binary search tree and has the property that for each nodeOf the tree, each element in its left subtree precedes the node element and eachElement in its right subtree succeeds the node element.

Page 38: Discrete Structures CISC 2315 FALL 2010 Graphs & Trees

Spanning Trees – Kruskal’s Algorithm

b2

2

2

2 2

31

3

11

11

a

c

d

e

f

g

Kruskal’s Algorithm:1) Sort the edges of the graph by weight, and let L be the sorted list2) Let T be the minimal spanning tree and initialize T : = 0.3) For each vertex v of the graph, create the equivalence class [v] = {v}.4) while there are 2 or more equivalence classes do

Let {a,b} be the edge at the head of L:L := tail (L);if [a} not = [b] then T:= T U {{a,b}}; Replace the equivalence classes [a] and [b] by [a] U [b]fi

od

L= {{a,b},{c,d},{d,g},{e,f},{f,g},{a,f},{b,c},{c,g},{d,e},{e,g},{a,g},{b,g}} 1 1 1 1 1 2 2 2 2 2 3 3

Spanning Tree T Equivalance Classes

{} {a},{b},{c},{d},{e},{f},{g}

{{a,b}} {a,b},{c},{d},{e},{f},{g}

{{a,b},{c,d}} {a,b},{c,d},{e},{f},{g}

{{a,b},{c,d},{d,g}} {a,b},{c,d,g},{e},{f}

{{a,b},{c,d},{d,g},{e,f}} {a,b},{c,d,g},{e,f}

{{a,b},{c,d},{d,g},{e,f},{f,g}} {a,b},{c,d,e,f,g}

{{a,b},{c,d},{d,g},{e,f},{f,g},{a,f}} {a,b,c,d,e,f,g}