midwestern state university department of computer science dr. ranette halverson cmps 2433 –...

35
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

Upload: valentina-kite

Post on 14-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

1

Midwestern State UniversityDepartment of Computer ScienceDr. Ranette Halverson

CMPS 2433 – CHAPTER 4

GRAPHS

Page 2: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

2

What is a graph?

Page 3: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

3

What is a graph?

Page 4: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

4

What is a graph?

Page 5: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

5

Graphs – Are they really important??

•Yes! Used in virtually EVERY discipline!•Common examples •Airline routes – draw lines between cities•Computer networks – lines for direct connection•Workforce – People & Jobs assigned•Travel – Cities & highways

Page 6: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

6

What is a graph?•Non-technical definition:•Set of objects in which some pairs are related•DEFN: Graph•Collection of Vertices (V) & Edges (E). Vertices are simple objects & an Edge is a connection between 2 Vertices •Non-empty, finite set of vertices (V) & set of edges (e) of 2-element subsets (pairs) of V

Note: Graph Theory terminology is not always consistent among books/authors

Page 7: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

7

What is a graph?•Author’s restrictions•No edge from vertex to itself•Only one edge between 2 specific vertices•Makes graphs simpler•But is BIG restriction•Other Examples???

Page 8: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

8

Graph Terminology•Vertices are generally named – a, b, c…•Edges usually named by 2 vertices – (a,b)•Edges may be labeled – usually a value •Value = distance, cost, etc.•Adjacent, Joined: 2 vertices are adjacent/joined if they are connected by an edge•Incident: a vertex & edge are incident if the edge connects to the vertex

Page 9: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

9

Example

•Vertex•Edge•Label•Value•Adjacent•Incident

Page 10: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

10

More Terms•Degree of Vertex: Number of edges incident to the vertex•Degree of Graph: Maximum of degrees of vertices•Complete Graph: Every vertex is adjacent to every other vertex• an edge exists between each pair of vertices

Page 11: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

11

Interpretation of a Graph•NOTE: The shape of a graph is not “important” it is the relationship that is significant.•A single graph can be drawn multiple ways but still represents the same relationships.

PROBLEM: Given a graph, can it be drawn without any of the edges crossing?

Page 12: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

12

Number of Vertices•How many edges are in a complete graph of N vertices? •Can we solve inductively? Can we develop the table?Vertices Edges

1 0

2 1

3 3

4 6

5 10

6 15

N ???

Page 13: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

13

Theorem 4.1•In a graph the sum of the degrees of the vertices equals twice the number of edges.•Why? Because each 1 edge is incident on 2 vertices.

Page 14: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

14

Graph Representation: Adjacency Matrix•Given a graph with N vertices, form an NXN matrix with rows & columns labeled to represent the vertices. Place a 1 in location (i, j) if there is an edge between Vertices i and j, and place a 0 otherwise. V1 V2 V3 V4

V1 0 1 1 0

V2 1 0 0 1

V3 1 0 0 1

V4 0 1 1 0

Page 15: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

15

Graph Representation: Adjacency List

List (array) containing each vertex, attached to a (linked) list of each vertex adjacent to it

V1 V2 V3

V2 V2 V4

V3 V1 V3

V4 V2 V3

Page 16: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

16

Isomorphic Graphs•Two graphs G1 & G2 are isomorphic if there exists a 1-to-1 correspondence f between the vertices of graphs such that if vertices u & v are adjacent in G1 then f(u) and f(v) are adjacent in G2•I.E. Graphs are the same except for labeling & arrangement

Page 17: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

17

Isomorphic Graphs•The following graphs are isomorphic•Colors indicate the 1-to-1 correspondence

Page 18: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

18

Determining Isomorphism•How could we determine if 2 graphs G1 & G2 are isomorphic?•This is a “hard” problem. (More later!)

•Can we determine if 2 Graphs G1 & G2 are NOT isomorphic?

Page 19: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

19

Determining Isomorphism

•Invariant – a term that states some fact that does not vary•State isomorphic graph invariants:•What properties must be true of 2 graphs if they are isomorphic.

Page 20: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

20

Determining Isomorphism

•Invariant – a term that states some fact that does not vary•State isomorphic graph invariants:•What properties must be true of 2 graphs if they are isomorphic?

•Equal number of vertices •Equal number of edges•Vertices have same degree

Page 21: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

21

Homework •Section 4.1•All 1 - 50

Page 22: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

22

4.2 Paths & Circuits•Multigraph: Graph which has parallel edges between vertices and/or loops •Loop: Edge from a vertex to itself•Degree is counted as 2 for a loop•Parallel Edges: Multiple edges between 2 vertices•Path: a path from vertices U to V is a sequence of vertices & edges from U to V•Simple Path: no repeated edge or vertex•Length of Path (from U to V): number of edges in the path from U to V

Page 23: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

23

Example•Multigraph•Loop•Degree (of node with loop)•Parallel Edges•Path•Length of Pat

Page 24: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

24

More Terminology•Connected: a graph in which there is a path between each pair of vertices•Cycle: •a path of length >0 •which begins & ends at the same vertex •all edges & non-ending vertices are distinct

Page 25: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

25

Euler Paths & Circuits

•Euler Path: a path including every edge exactly once & different first & last vertices•Euler Circuit: a path including every edge exactly once & the same first & last vertices•See examples on pages 165 - 168

Page 26: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

26

Existence of Euler Circuit•Graph must be connected•Each vertex reached must be exited – thus must have degree 2 (in & out) •If a vertex is visited multiple times must have even number of vertices•Example 4.20 – page 168

•Euler Circuit Algorithm (pg. 169-170)

Page 27: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

27

Euler Circuit Algorithm - Overview•Select any vertex – U•Randomly select unused edges to construct circuit from U to U•If unselected edges remain, •Select a vertex with unselected edge & construct a circuit•Repeat until all edges are includedNote: not a unique solution

Page 28: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

28

Euler Circuit - Implementation•How is graph represented?•How do you mark edges as selected?•How do you know if unselected edges remain?•Sets…•E is set of edges•P is path

Page 29: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

29

Theorem 4.5 Necessary & sufficient conditions for Euler circuit - path

Suppose a multigraph G is connected. • G has an Euler circuit iff every vertex

has even degree.• G has an Euler path iff every vertex

has even degree except for 2 distinct vertices which have odd degree (Path begins at one odd vertex & ends at the other)

Page 30: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

30

Complexity of Euler Circuit Algorithm for a connected graph

•Let “selecting an edge” be a basic operation. Since e edges, complexity is O(e)•For graph with n vertices, what is max e for any given vertex?

e <= ½ n (n-1) = ½ (n2 – n) O(n2)

What about a multigraph?

Page 31: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

31

Hamiltonial Cycles & Paths

•Hamiltonian Path: Path containing each vertex of a graph exactly once•Hamiltonian Cycle: Cycle containing each vertex of a graph exactly once•Figures 4.1 (154) & 4.22 (172)

Page 32: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

32

Existence of Hamiltonian Cyles & Paths•Unsolved Problem: necessary & sufficient conditions for Hamiltonian Cycle & Path•Hard to find even if know of existence

Theorem 4.6S’pose G is a graph with n >2

vertices. If each pair of non-adjacent vertices U & V satisfy deg(U) +

deg(V) >= nthen G has a Hamiltonian Cycle.

Page 33: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

33

Extension of Theorem 4.6Existence of Hamiltonial Cycle

S’pose graph G has n vertices and every vertex has degree >= n/2. G has a Hamiltonial Cycle.

WHY???But still have no algorithm for finding it.

Page 34: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

34

More on Theorem 4.6•Failing Theorem 5.6 does not mean a Hamiltonian Cycle does not exist.•None of non-adjacent vertices have degree n/2, but HamiltonianCycle exists

Page 35: Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1

35

Homework Section 4.2•Page 176 +•Problems 1 – 29, 35 – 38, 60

•Rest of chapter in separate file