Week 7 – Simple Graph
Algorithms
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
What is a Graph
• A graph is a finite set of vertices that can be connected by undirected/directed
Vertex
• Vertex
– Represented as a circle – Can also be called a node
– Usually has some representational meaning
Edge
• Edge
– Represented as a line – Connects two vertices
– Can have direction and weight
• Arc = directed edge
– Shown in red so arrowheads are visible
5
Face
• An area enclosed by edges and vertices • Only for planar graphs
• Don’t forget the outside face
6
1
2
How Many Vertices, Edges and Faces?
7
• V = ? • F = ?
How Many Vertices, Edges and Faces?
8
• V = 5 • F = 2
• E = 5
How Many Vertices, Edges and Faces?
9
• V = ? • F = ?
How Many Vertices, Edges and Faces?
10
• V = 6 • F = 5
• E = 9
How Many Vertices, Edges and Faces?
11
• V = ? • F = ?
How Many Vertices, Edges and Faces?
12
• V = 4 • F = 2
• E = 4
How Many Vertices, Edges and Faces?
13
• V = ? • F = ?
How Many Vertices, Edges and Faces?
14
• V = 4 • F = 4
• E = 6
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
Euler's Formula
• For planar graphs • V + F = E + 2
• V + F – E = 2
Planar Graph
• A graph that can be drawn with no edges
crossing each other
Is This Graph Planar?
Is This Graph Planar?
19
Is This Graph Planar?
Is This Graph Planar?
21
Is This Graph Planar?
22
Is This Graph Planar?
Is This Graph Planar?
24
Is This Graph Planar?
Is This Graph Planar?
26
Is This Graph Planar?
Is This Graph Planar?
28
Algorithm to determine planarity
• Auslander and Parter (1961; Skiena 1990, p. 247). • O(n^3)
Untangle
• Fun game
• Try to draw the graph as a planar graph
• http://www.addictinggames.com/puzzle-games
/untangle.jsp
Sparseness
• Planar graphs are sparse
– Most vertices do not share an edge
• Matrix is mostly empty
– Mostly 0’s
Origami
• Planar graphs of 3D figures • Map out graph on 2D surface
• Create 3D figure
Example 1 - Hexahedron
Example 2 – Bucky ball (Hexagons = 0)
Example 3 – Bucky ball (Hexagons = 20)
Undirected Graph
• A graph that does not have any directed edges • Implied that vertices have no self-edges
Directed Graph
• A graph that has at least one directed edge
37
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
What is the Degree of Vertex A?
39
What is the Degree of this Vertex?
40
A
What is the degree of this graph?
41
Is this Graph Regular?
43
No
Is This Graph Regular?
Is This Graph Regular?
45
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
Path Example
48
A B C D
E F G H
Is There a Path From A to L?
49
A B C D
E F G H
Is There a Path From A to L?
50
Yes
A B C D
E F G H
Is There a Path From A to H?
51
A B C D
E F G H
Is There a Path From A to H?
52
No
A B C D
E F G H
Path Example
53
A B C D
E F G H
Is There a Path From L to A
54
A B C D
E F G H
Is There a Path From L to A
55
No
A B C D
E F G H
Is There a Path From A to L
56
A B C D
E F G H
Is There a Path From A to L
57
Yes
A B C D
E F G H
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
Trail Example
59
A B C D
E F G H
Find Some Trails Starting at A and Ending at
D
60
A B C D
E F G H
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
Cycle
• A cycle is a path that starts and ends at the same vertex
• A B C A • A C B A • B A C B • B C A B • C A B C • C B A C
Directed Cycle Example
63
• A B C A
• B C A B
• C A B C
A
B
Circuit
• A circuit is a trail that starts and ends at the
same vertex
Find Some Circuits Starting at G
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
Connected
• A graph is connected if there exists an
undirected path from every vertex to every other vertex
Is This Graph Connected
68
A B C D
E F G H
Is This Graph Connected?
69
No
A B C D
E F G H
Tree
• A tree is a connected graph with no cycles
Tree
71
A B C D
E F G H
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
Is This Graph Complete?
Is This Graph Complete?
75
Is This Graph Complete?
Is This Graph Complete?
77
Is This Graph Complete?
Is This Graph Complete?
79
Hamilton Cycle
• A cycle that starts and ends at the same vertex
and visits all other vertices exactly once
Hamilton Cycle Example
• Red + Blue lines together = Hamilton cycle
Hamilton Cycle Example 2
• Red + Green lines together = Hamilton cycle
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
Does There Exists an Eulerian Path?
84
A B C D
E F G H
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-degree – Exactly two vertices have odd degree
Does There Exists an Eulerian Path?
86
A B C D
E F G H
Does There Exists an Eulerian Path?
87
A B C D
E F G H
Does There Exists an Eulerian Path?
88
A B C D
E F G H
Does There Exists an Eulerian Path?
89
A B C D
E F G H
Does There Exists an Eulerian Path?
90
A B C D
E F G H
Does There Exists an Eulerian Path?
91
A B C D
E F G H
Does There Exists an Eulerian Path?
92
A B C D
E F G H
Does There Exists an Eulerian Path?
93
A B C D
E F G H
Does There Exists an Eulerian Path?
94
A B C D
E F G H
Does There Exists an Eulerian Path?
95
A B C D
E F G H
Does There Exists an Eulerian Path?
96
A B C D
E F G H
Does There Exists an Eulerian Path?
97
A B C D
E F G H
Does There Exists an Eulerian Path?
98
A B C D
E F G H
Does There Exists an Eulerian Path?
99
A B C D
E F G H
Does There Exists an Eulerian Path?
100
A B C D
E F G H
Does There Exists an Eulerian Path?
101
A B C D
E F G H
Does There Exists an Eulerian Circuit?
102
• An Eulerian circuit exists if
• Undirected
– All vertices have even degree
• Directed
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
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
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
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
Algorithms Using Adjacency Matrices
• Finding a path between two vertices • Finding a trail between two vertices
– Same as path for undirected graphs – May be different for directed graphs
• Does the graph have a cycle • Does the graph have a circuit
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