10/14/20151 graph theory. 10/14/20152 9.1: what are graphs? general meaning in everyday math: a plot...

43
03/22/22 1 Graph Theory

Upload: bethany-underwood

Post on 31-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

04/19/23 1

Graph Theory

04/19/23 2

9.1: What are Graphs?

• General meaning in everyday math: General meaning in everyday math: A plot or chart of numerical data using a A plot or chart of numerical data using a coordinate system.coordinate system.

• Technical meaning in discrete mathematics:Technical meaning in discrete mathematics:A particular class of discrete structures (to A particular class of discrete structures (to be defined) that is useful for representing be defined) that is useful for representing relations and has a convenient webby-relations and has a convenient webby-looking graphical representation.looking graphical representation.

04/19/23 3

Applications of Graphs

• Potentially anything (graphs can represent Potentially anything (graphs can represent relations, relations can describe the relations, relations can describe the extension of any predicate).extension of any predicate).

• Apps in networking, scheduling, flow Apps in networking, scheduling, flow optimization, circuit design, path planning.optimization, circuit design, path planning.

• Geneology analysis, computer game-Geneology analysis, computer game-playing, program compilation, object-playing, program compilation, object-oriented design, …oriented design, …

04/19/23 4

Types of Graphs:1. Simple Graphs

• Correspond to symmetricCorrespond to symmetricbinary relations binary relations RR..

• A A simple graphsimple graph GG=(=(VV,,EE))consists of:consists of:– a set a set VV of of verticesvertices or or nodes nodes ( (VV corresponds to corresponds to

the universe of the relation the universe of the relation RR),),– a set a set EE of of edgesedges / / arcsarcs / / linkslinks: unordered pairs : unordered pairs

of of [distinct?][distinct?] elements elements uu,,vv VV, such that , such that uRvuRv..

Visual Representationof a Simple Graph

2 3

1

04/19/23 5

• Let Let VV be the set of states in the far- be the set of states in the far-southeastern U.S.:southeastern U.S.:–VV={FL, GA, AL, MS, LA, SC, TN, NC}={FL, GA, AL, MS, LA, SC, TN, NC}

• Let Let EE={{={{uu,,vv}|}|u u adjoins adjoins vv}}={{FL,GA},{FL,AL},{FL,MS},={{FL,GA},{FL,AL},{FL,MS},{FL,LA},{GA,AL},{AL,MS},{FL,LA},{GA,AL},{AL,MS},{MS,LA},{GA,SC},{GA,TN},{MS,LA},{GA,SC},{GA,TN},{SC,NC},{NC,TN},{MS,TN},{SC,NC},{NC,TN},{MS,TN},{MS,AL}}{MS,AL}}

Example of a Simple Graph

TN

ALMS

LA

SC

GA

FL

NC

04/19/23 6

2. Multigraphs

• Like simple graphs, but there may be Like simple graphs, but there may be more more than onethan one edge connecting two given nodes. edge connecting two given nodes.

• A A multigraphmultigraph GG=(=(VV, , EE, , f f ) consists of a set ) consists of a set VV of vertices, a set of vertices, a set EE of edges (as primitive of edges (as primitive objects), and a functionobjects), and a functionff::EE{{{{uu,,vv}|}|uu,,vvVV uuvv}.}.

• E.g., nodes are cities, edgesE.g., nodes are cities, edgesare segments of major highways.are segments of major highways.

Paralleledges

04/19/23 7

3. Pseudographs

• Like a multigraph, but edges connecting a Like a multigraph, but edges connecting a node to itself are allowed.node to itself are allowed.

• A A pseudographpseudograph GG=(=(VV, , EE, , f f ) where) whereff::EE{{{{uu,,vv}|}|uu,,vvVV}. Edge }. Edge eeEE is a is a looploop if if ff((ee)={)={uu,,uu}={}={uu}.}.

• E.g.E.g., nodes are campsites, nodes are campsitesin a state park, edges arein a state park, edges arehiking trails through the woods.hiking trails through the woods.

04/19/23 8

Directed Graphs

• Correspond to arbitrary binary relations Correspond to arbitrary binary relations RR, , which need not be symmetric.which need not be symmetric.

• A A directed graphdirected graph ( (VV,,EE) consists of a set of ) consists of a set of vertices vertices VV and a binary relation and a binary relation EE on on VV..

• E.g.E.g.: : VV = people, = people,EE={(={(xx,,yy) | ) | xx loves loves yy}}

04/19/23 9

Directed Multigraphs

• Like directed graphs, but there may be more Like directed graphs, but there may be more than one arc from a node to another.than one arc from a node to another.

• A A directed multigraphdirected multigraph GG=(=(VV, , EE, , f f ) consists ) consists of a set of a set VV of vertices, a set of vertices, a set EE of edges, and a of edges, and a function function ff::EEVVVV..

• E.g., E.g., VV=web pages,=web pages,EE=hyperlinks. =hyperlinks. The WWW isThe WWW isa directed multigraph...a directed multigraph...

04/19/23 10

Types of Graphs: Summary

• Summary of the book’s definitions.Summary of the book’s definitions.

• Keep in mind this terminology is not fully Keep in mind this terminology is not fully standardized...standardized...

TermEdgetype

Multipleedges ok?

Self-loops ok?

Simple graph Undir. No NoMultigraph Undir. Yes NoPseudograph Undir. Yes YesDirected graph Directed No YesDirected multigraph Directed Yes Yes

04/19/23 11

9.2: Graph Terminology

• Adjacent, connects, endpoints, degree, Adjacent, connects, endpoints, degree, initial, terminal, in-degree, out-degree, initial, terminal, in-degree, out-degree, complete, cycles, wheels, n-cubes, bipartite, complete, cycles, wheels, n-cubes, bipartite, subgraph, union.subgraph, union.

04/19/23 12

Adjacency

Let Let GG be an undirected graph with edge set be an undirected graph with edge set EE. . Let Let eeEE be (or map to) the pair { be (or map to) the pair {uu,,vv}. Then }. Then we say:we say:

• uu, , vv are are adjacentadjacent / / neighborsneighbors / / connectedconnected..

• Edge Edge ee is is incident withincident with vertices vertices uu and and vv..

• Edge Edge ee connectsconnects uu and and vv..

• Vertices Vertices uu and and vv are are endpointsendpoints of edge of edge ee..

04/19/23 13

Degree of a Vertex

• Let Let GG be an undirected graph, be an undirected graph, vvVV a vertex. a vertex.

• The The degreedegree of of vv, deg(, deg(vv), is its number of ), is its number of incident edges. (Except that any self-loops incident edges. (Except that any self-loops are counted twice.)are counted twice.)

• A vertex with degree 0 is A vertex with degree 0 is isolatedisolated..

• A vertex of degree 1 is A vertex of degree 1 is pendantpendant..

04/19/23 14

Handshaking Theorem

• Let Let GG be an undirected (simple, multi-, or be an undirected (simple, multi-, or pseudo-) graph with vertex set pseudo-) graph with vertex set VV and edge and edge set set EE. Then. Then

• Corollary: Any undirected graph has an Corollary: Any undirected graph has an even number of vertices of odd degree.even number of vertices of odd degree.

EvVv

2)deg(

04/19/23 15

• deg(a)deg(a) = 6 = 6• deg(b)deg(b) = 4 = 4• deg( c)deg( c) = 1 = 1 pendantpendant• deg(d)deg(d) = 0 = 0 isolatedisolated• deg(e) = 3deg(e) = 3• deg(f) = 4deg(f) = 4• deg(g) = 2deg(g) = 2• ∑∑deg(v) = 20 = 2∑edges=2 x 10 deg(v) = 20 = 2∑edges=2 x 10

04/19/23 16

Directed Adjacency

• Let Let GG be a directed (possibly multi-) graph, be a directed (possibly multi-) graph, and let and let ee be an edge of be an edge of GG that is (or maps that is (or maps to) (to) (uu,,vv). Then we say:). Then we say:– uu is is adjacent toadjacent to vv, , vv is is adjacent fromadjacent from uu– ee comes fromcomes from u, e u, e goes togoes to v. v.– e connects u to ve connects u to v, , e goes from u to ve goes from u to v– the the initial vertexinitial vertex of of ee is is uu– the the terminal vertexterminal vertex of of ee is is vv

04/19/23 17

Directed Degree

• Let Let GG be a directed graph, be a directed graph, vv a vertex of a vertex of GG..– The The in-degreein-degree of of vv, deg, deg((vv), is the number of ), is the number of

edges going to edges going to vv..– The The out-degreeout-degree of of vv, deg, deg((vv), is the number of ), is the number of

edges coming from edges coming from vv..– The The degreedegree of of vv, deg(, deg(vv))degdeg((vv)+deg)+deg((vv), is the ), is the

sum of sum of vv’s in-degree and out-degree.’s in-degree and out-degree.

04/19/23 18

Directed Handshaking Theorem

• Let Let GG be a directed (possibly multi-) graph be a directed (possibly multi-) graph with vertex set with vertex set VV and edge set and edge set EE. Then:. Then:

• Note that the degree of a node is unchanged Note that the degree of a node is unchanged by whether we consider its edges to be by whether we consider its edges to be directed or undirected.directed or undirected.

EvvvVvVvVv

)deg(2

1)(deg)(deg

04/19/23 19

• deg+(a) = 3deg+(a) = 3 deg -(a) = 3 deg -(a) = 3• deg+(b) = 3 deg -(b) = 1deg+(b) = 3 deg -(b) = 1• deg+( c) = 0 deg -(c) = 1deg+( c) = 0 deg -(c) = 1• deg+(d) = 0 deg -(d) = 0deg+(d) = 0 deg -(d) = 0• deg+(e) = 1deg+(e) = 1 deg-(e) = 2deg-(e) = 2• deg+(f) = 2deg+(f) = 2 deg-(f) = 2deg-(f) = 2• deg+(g) = 1deg+(g) = 1 deg-(g) = 1deg-(g) = 1• ∑∑deg+(v) = ∑deg-(v) =1/2 ∑deg(v)= ∑edges = deg+(v) = ∑deg-(v) =1/2 ∑deg(v)= ∑edges =

10 10

04/19/23 20

Special Graph Structures

Special cases of undirected graph structures:Special cases of undirected graph structures:

• Complete graphs Complete graphs KKnn

• Cycles Cycles CCnn

• Wheels Wheels WWnn

• nn-Cubes -Cubes QQnn

• Bipartite graphsBipartite graphs

• Complete bipartite graphs Complete bipartite graphs KKmm,,nn

04/19/23 21

Complete Graphs

• For any For any nnNN, a , a complete graphcomplete graph on on nn vertices, vertices, KKnn, is a simple graph with , is a simple graph with nn nodes nodes

in which every node is adjacent to every in which every node is adjacent to every other node: other node: uu,,vvVV: : uuvv{{uu,,vv}}EE..

K1 K2K3

K4 K5 K6Note that Kn has edges.

2

)1(`

1

nni

n

i

04/19/23 22

Cycles

• For any For any nn3, a 3, a cyclecycle on on nn vertices, vertices, CCnn, is a , is a

simple graph where simple graph where VV={={vv11,,vv22,… ,,… ,vvnn} and } and

EE={{={{vv11,,vv22},{},{vv22,,vv33},…,{},…,{vvnn11,,vvnn},{},{vvnn,,vv11}}.}}.

C3 C4 C5 C6 C7C8

How many edges are there in Cn?

04/19/23 23

Wheels

• For any For any nn3, a 3, a wheelwheel WWnn, is a simple graph , is a simple graph

obtained by taking the cycle obtained by taking the cycle CCnn and adding and adding

one extra vertex one extra vertex vvhubhub and and nn extra edges extra edges

{{{{vvhubhub,,vv11}, {}, {vvhubhub,,vv22},…,{},…,{vvhubhub,,vvnn}}.}}.

W3 W4 W5 W6 W7W8

How many edges are there in Wn?

04/19/23 24

n-cubes (hypercubes)

• For any For any nnNN, the hypercube , the hypercube QQnn is a simple is a simple

graph consisting of two copies of graph consisting of two copies of QQnn-1-1

connected together at corresponding nodes. connected together at corresponding nodes. QQ00 has 1 node. has 1 node.

Q0Q1 Q2 Q3

Q4

Number of vertices: 2n. Number of edges:Exercise to try!

04/19/23 25

n-cubes (hypercubes)

• For any For any nnNN, the hypercube , the hypercube QQnn can be can be

defined recursively as follows:defined recursively as follows:– QQ00={{={{vv00},},} (one node and no edges)} (one node and no edges)

– For any For any nnNN, if Q, if Qnn==((VV,,EE), where ), where VV={={vv11,…,,…,vvaa} }

andand E E={={ee11,…,,…,eebb}, then }, then QQnn+1+1=(=(VV{{vv11´,…,´,…,vvaa´}, ´},

EE{{ee11´,…,´,…,eebb´}´}{{{{vv11,,vv11´},{´},{vv22,,vv22´},…,´},…,

{{vvaa,,vvaa´}}) where ´}}) where vv11´,…,´,…,vvaa´ are new vertices, and ´ are new vertices, and

where if where if eeii={={vvjj,,vvkk} then } then eeii´={´={vvjj´,´,vvkk´}. ´}.

04/19/23 26

Subgraphs

• A subgraph of a graph A subgraph of a graph GG=(=(VV,,EE) is a graph ) is a graph HH=(=(WW,,FF) where ) where WWVV and and FFEE..

G H

04/19/23 27

Graph Unions

• The The unionunion GG11GG22 of two simple graphs of two simple graphs

GG11=(=(VV11, , EE11) and ) and GG22=(=(VV22,,EE22) is the simple ) is the simple

graph (graph (VV11VV22, , EE11EE22).).

04/19/23 28

9.3: Graph Representations

• Graph representations:Graph representations:– Adjacency lists.Adjacency lists.– Adjacency matrices.Adjacency matrices.– Incidence matrices.Incidence matrices.

04/19/23 29

Adjacency Lists

• A table with 1 row per vertex, listing its A table with 1 row per vertex, listing its adjacent vertices.adjacent vertices.

a b

dc

fe

VertexAdjacentVertices

ab

b, ca, c, e, f

c a, b, fde bf c, b

04/19/23 30

Directed Adjacency Lists

• 1 row per node, listing the terminal nodes of 1 row per node, listing the terminal nodes of each edge incident from that node.each edge incident from that node.

04/19/23 31

Adjacency Matrices

• Matrix Matrix AA=[=[aaijij], where ], where aaijij is 1 if { is 1 if {vvii, , vvjj} is an } is an

edge of edge of GG, 0 otherwise., 0 otherwise.

a b

dc e

0001000000000111010100110

edcba

a b c d e

04/19/23 32

Adjacency Matrices

• Matrix Matrix AA=[=[aaijij], where ], where aaijij is 1 if { is 1 if {vvii, , vvjj} is an } is an

edge of edge of GG, 0 otherwise., 0 otherwise.

a b

dc e

0001000000000100000000110

edcba

a b c d e

04/19/23 33

§8.4: Connectivity

• In an undirected graph, a In an undirected graph, a path of length n path of length n from u to vfrom u to v is a sequence of adjacent edges is a sequence of adjacent edges going from vertex going from vertex u u to vertex to vertex vv..

• A path is a A path is a circuitcircuit if if u=vu=v..

• A path A path traversestraverses the vertices along it. the vertices along it.

• A path is A path is simplesimple if it contains no edge more if it contains no edge more than once.than once.

04/19/23 34

Paths in Directed Graphs

• Same as in undirected graphs, but the path Same as in undirected graphs, but the path must go in the direction of the arrows.must go in the direction of the arrows.

04/19/23 35

§9.1: Introduction to Trees

• A A treetree is a connected undirected graph with is a connected undirected graph with no simple circuits.no simple circuits.– Theorem: Theorem: There is a unique simple path There is a unique simple path

between any two of its nodes.between any two of its nodes.

• An undirected graph without simple circuits An undirected graph without simple circuits is called a is called a forestforest..– You can think of it as a set of trees having You can think of it as a set of trees having

disjoint sets of nodes.disjoint sets of nodes.

04/19/23 36

Rooted Trees

• A A rooted treerooted tree is a tree in which one node is a tree in which one node has been designated the has been designated the rootroot..– Every edge is (implicitly or explicitly) directed Every edge is (implicitly or explicitly) directed

away from the root.away from the root.

• You should know the following terms about You should know the following terms about rooted trees:rooted trees:– Parent, child, siblings, ancestors, descendents, Parent, child, siblings, ancestors, descendents,

leaf, internal node, subtree.leaf, internal node, subtree.

04/19/23 37

n-ary trees

• A rooted tree is called A rooted tree is called nn-ary if every -ary if every internal vertex has no more than internal vertex has no more than nn children. children.

• It is It is fullfull if every internal vertex has if every internal vertex has exactlyexactly nn children. children.

• A 2-ary tree is called a A 2-ary tree is called a binary treebinary tree..

04/19/23 38

Ordered Rooted Tree

• A rooted tree where the children of each A rooted tree where the children of each internal node are ordered.internal node are ordered.

• In ordered binary trees, we can define:In ordered binary trees, we can define:– left child, right childleft child, right child– left subtree, right subtreeleft subtree, right subtree

• For For nn-ary trees with -ary trees with nn>2, can use terms like >2, can use terms like “leftmost”, “rightmost,” etc. “leftmost”, “rightmost,” etc.

04/19/23 39

Trees as Models

• Can use trees to model the following:Can use trees to model the following:– Saturated hydrocarbonsSaturated hydrocarbons– Organizational structuresOrganizational structures– Computer file systemsComputer file systems

• In each case, would you use a rooted or a In each case, would you use a rooted or a non-rooted tree?non-rooted tree?

04/19/23 40

Some Tree Theorems

• A tree with A tree with nn nodes has nodes has nn−1 edges.−1 edges.

• A full A full mm-ary tree with -ary tree with ii internal nodes has internal nodes has nn==mimi+1 nodes, and +1 nodes, and =(=(mm−1)−1)ii+1 leaves.+1 leaves.– Proof: There are Proof: There are mimi children of internal nodes, children of internal nodes,

plus the root. And, plus the root. And, = = nn−−ii = ( = (mm−1)−1)ii+1. □ +1. □ – Thus, given Thus, given mm, we can compute any of , we can compute any of ii, , nn, and , and

from any of the others. from any of the others.

04/19/23 41

More Theorems

• Definition:Definition: The The levellevel of a node is the length of the of a node is the length of the simple path from the root to the node.simple path from the root to the node.– The The heightheight of a tree is maximum node level. of a tree is maximum node level.

– A rooted A rooted mm-ary tree with height -ary tree with height hh is is balancedbalanced if all if all leaves are at levels leaves are at levels hh or or hh−1.−1.

• Theorem: Theorem: There are at most There are at most mmhh leaves in an leaves in an mm--ary tree of height ary tree of height hh..– Corollary:Corollary: An An mm-ary tree with -ary tree with leaves has height leaves has height

hh≥≥loglogmm . If . If mm is full and balanced then is full and balanced then hh==loglogmm

04/19/23 42

§9.2: Applications of Trees

• Binary search treesBinary search trees

• Decision treesDecision trees– Minimum comparisons in sorting algorithmsMinimum comparisons in sorting algorithms

• Prefix codesPrefix codes– Huffman codingHuffman coding

• Game treesGame trees

04/19/23 43

§9.3: Tree Traversal

• Traversal algorithmsTraversal algorithms– Depth-first traversal:Depth-first traversal:

• Preorder traversalPreorder traversal• Inorder traversalInorder traversal• Postorder traversalPostorder traversal

– Breadth-first traversalBreadth-first traversal

• Infix/prefix/postfix notationInfix/prefix/postfix notation