• No results found

GraphTheoryBasics

N/A
N/A
Protected

Academic year: 2020

Share "GraphTheoryBasics"

Copied!
108
0
0

Loading.... (view fulltext now)

Full text

(1)

Week 7 – Simple Graph

Algorithms

(2)

A Few Graph Theory Applications

Software tools (code flow, etc…)

McCabe IQ tool

– Visual Paradigm

• Networks

– Flow, paths, etc…

• Game AI

Path algorithms

– Decision algorithms

Origami

– Planar models of 3d figures

Modeling in general

– Decision trees, Bayesian networks, neural networks

(3)

What is a Graph

• A graph is a finite set of vertices that can be connected by undirected/directed

(4)

Vertex

Vertex

Represented as a circleCan also be called a node

Usually has some representational meaning

(5)

Edge

• Edge

Represented as a lineConnects two vertices

– Can have direction and weight

• Arc = directed edge

– Shown in red so arrowheads are visible

5

(6)

Face

An area enclosed by edges and vertices • Only for planar graphs

• Don’t forget the outside face

6

1

2

(7)

How Many Vertices, Edges and Faces?

7

V = ? • F = ?

(8)

How Many Vertices, Edges and Faces?

8

V = 5 • F = 2

• E = 5

(9)

How Many Vertices, Edges and Faces?

9

V = ? • F = ?

(10)

How Many Vertices, Edges and Faces?

10

V = 6 • F = 5

• E = 9

(11)

How Many Vertices, Edges and Faces?

11

V = ? • F = ?

(12)

How Many Vertices, Edges and Faces?

12

V = 4 • F = 2

• E = 4

(13)

How Many Vertices, Edges and Faces?

13

V = ? • F = ?

(14)

How Many Vertices, Edges and Faces?

14

V = 4 • F = 4

• E = 6

(15)

Relationship between V, F, and E

15

What is the pattern/relationship?

V F E

Graph 1 5 2 5

Graph 2 6 5 9

Graph 3 4 2 4

(16)

Euler's Formula

For planar graphs • V + F = E + 2

• V + F – E = 2

(17)

Planar Graph

A graph that can be drawn with no edges

crossing each other

(18)

Is This Graph Planar?

(19)

Is This Graph Planar?

19

(20)

Is This Graph Planar?

(21)

Is This Graph Planar?

21

(22)

Is This Graph Planar?

22

(23)

Is This Graph Planar?

(24)

Is This Graph Planar?

24

(25)

Is This Graph Planar?

(26)

Is This Graph Planar?

26

(27)

Is This Graph Planar?

(28)

Is This Graph Planar?

28

(29)

Algorithm to determine planarity

Auslander and Parter (1961; Skiena 1990, p. 247).O(n^3)

(30)

Untangle

Fun game

• Try to draw the graph as a planar graph

• http://www.addictinggames.com/puzzle-games

/untangle.jsp

(31)

Sparseness

Planar graphs are sparse

Most vertices do not share an edge

Matrix is mostly empty

Mostly 0’s

(32)

Origami

Planar graphs of 3D figures • Map out graph on 2D surface

• Create 3D figure

(33)

Example 1 - Hexahedron

(34)

Example 2 – Bucky ball (Hexagons = 0)

(35)

Example 3 – Bucky ball (Hexagons = 20)

(36)

Undirected Graph

A graph that does not have any directed edges • Implied that vertices have no self-edges

(37)

Directed Graph

A graph that has at least one directed edge

37

(38)

Degree

The degree of a vertex is the number of edges

it has

• The degree of a graph is the same as the maximum degree of all the vertices

• In a regular graph all vertices have the same degree

Degree of a graph often refers to regular graphs

(39)

What is the Degree of Vertex A?

39

(40)

What is the Degree of this Vertex?

40

A

(41)

What is the degree of this graph?

41

(42)

Is this Graph Regular?

(43)

43

No

(44)

Is This Graph Regular?

(45)

Is This Graph Regular?

45

(46)
(47)

Path

A path is traveling on the edges of the graph

without visiting the same vertex more than once

• First vertex on the path can be the same as the last

Paths do not have to end where they started

(48)

Path Example

48

A B C D

E F G H

(49)

Is There a Path From A to L?

49

A B C D

E F G H

(50)

Is There a Path From A to L?

50

Yes

A B C D

E F G H

(51)

Is There a Path From A to H?

51

A B C D

E F G H

(52)

Is There a Path From A to H?

52

No

A B C D

E F G H

(53)

Path Example

53

A B C D

E F G H

(54)

Is There a Path From L to A

54

A B C D

E F G H

(55)

Is There a Path From L to A

55

No

A B C D

E F G H

(56)

Is There a Path From A to L

56

A B C D

E F G H

(57)

Is There a Path From A to L

57

Yes

A B C D

E F G H

(58)

Trail

A trail is traveling the edges of a graph

without using the same edge more than once

• Can use a vertex more than once

Trails do not have to end at the same vertex it

starts at

(59)

Trail Example

59

A B C D

E F G H

(60)

Find Some Trails Starting at A and Ending at

D

60

A B C D

E F G H

(61)

Find Some Trails Starting at A and Ending at

D

61

A  B  G  D

A  B  G  K  L  G  D

A  B  G  J  I  E  F  C  H  G  D

A B C D

E F G H

(62)

Cycle

A cycle is a path that starts and ends at the same vertex

A  B  C  A • A C B AB A C BB  C  A  B • C  A  B  C • C B A C

(63)

Directed Cycle Example

63

• A  B  C  A

• B  C  A  B

• C  A  B  C

A

B

(64)

Circuit

A circuit is a trail that starts and ends at the

same vertex

(65)

Find Some Circuits Starting at G

(66)

Find Some Circuits Starting at G

66

G K L G

G  J  I  E  F  C  H  G • G  C  H  G

A B C D

E F G H

(67)

Connected

A graph is connected if there exists an

undirected path from every vertex to every other vertex

(68)

Is This Graph Connected

68

A B C D

E F G H

(69)

Is This Graph Connected?

69

No

A B C D

E F G H

(70)

Tree

A tree is a connected graph with no cycles

(71)

Tree

71

A B C D

E F G H

(72)
(73)

Complete

A complete graph is an undirected graph

where every vertex has an edge to every other vertex

All edges possible are in the graph

(74)

Is This Graph Complete?

(75)

Is This Graph Complete?

75

(76)

Is This Graph Complete?

(77)

Is This Graph Complete?

77

(78)

Is This Graph Complete?

(79)

Is This Graph Complete?

79

(80)

Hamilton Cycle

A cycle that starts and ends at the same vertex

and visits all other vertices exactly once

(81)

Hamilton Cycle Example

Red + Blue lines together = Hamilton cycle

(82)

Hamilton Cycle Example 2

• Red + Green lines together = Hamilton cycle

(83)

Eulerian Circuit (cycle)

A circuit that starts and ends at the same

vertex and uses every edge in the graph exactly once

http://

mathworld.wolfram.com/EulerianCycle.html

(84)

Does There Exists an Eulerian Path?

84

A B C D

E F G H

(85)

Does There Exists an Eulerian Path?

85

• An Eulerian path exists if

• Undirected

All vertices have even degree

• Directed

All vertices have the same in-degree as out-degreeExactly two vertices have odd degree

(86)

Does There Exists an Eulerian Path?

86

A B C D

E F G H

(87)

Does There Exists an Eulerian Path?

87

A B C D

E F G H

(88)

Does There Exists an Eulerian Path?

88

A B C D

E F G H

(89)

Does There Exists an Eulerian Path?

89

A B C D

E F G H

(90)

Does There Exists an Eulerian Path?

90

A B C D

E F G H

(91)

Does There Exists an Eulerian Path?

91

A B C D

E F G H

(92)

Does There Exists an Eulerian Path?

92

A B C D

E F G H

(93)

Does There Exists an Eulerian Path?

93

A B C D

E F G H

(94)

Does There Exists an Eulerian Path?

94

A B C D

E F G H

(95)

Does There Exists an Eulerian Path?

95

A B C D

E F G H

(96)

Does There Exists an Eulerian Path?

96

A B C D

E F G H

(97)

Does There Exists an Eulerian Path?

97

A B C D

E F G H

(98)

Does There Exists an Eulerian Path?

98

A B C D

E F G H

(99)

Does There Exists an Eulerian Path?

99

A B C D

E F G H

(100)

Does There Exists an Eulerian Path?

100

A B C D

E F G H

(101)

Does There Exists an Eulerian Path?

101

A B C D

E F G H

(102)

Does There Exists an Eulerian Circuit?

102

• An Eulerian circuit exists if

• Undirected

All vertices have even degree

• Directed

(103)

How to Represent Graphs

Multidimensional arrays / Adjacency matrix

Better for dense graphs

Dense = lots of edges

• Edge list

Better for sparse graphs

• Sparse = few edges

(104)

Adjacency Matrix

An N X N matrix where N is the number of

vertices

• A ‘0’ represents to edge between the vertices

A ‘1’ represents an edge between the vertices

(105)

Undirected Adjacency Matrix

105

0 1 2 3 0 0 0 1 1

1 0 0 0 1

2 1 0 0 1

3 1 1 1 0

0

2 3

(106)

Directed Adjacency Matrix

106

0

2 3

1

0 1 2 3 0 0 0 1 1

1 0 0 0 1

2 0 0 0 1

(107)

Algorithms Using Adjacency Matrices

Finding a path between two vertices • Finding a trail between two vertices

Same as path for undirected graphsMay be different for directed graphs

Does the graph have a cycle • Does the graph have a circuit

(108)

Algorithms Using Adjacency Matrices

Is the graph connected • Is the graph a tree

• Is the graph complete

• Does a graph have a Hamilton cycle – And what is the cycle

• Does a graph have an Eulerian circuit – And what is the circuit

http://www.addictinggames.com/puzzle-games/untangle.jsp http://mathworld.wolfram.com/EulerianCycle.html

References

Related documents