Graphs: Erin Keith

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 52

Graphs

ERIN KEITH

21_GRAPHS 1
Topics
1. Background
2. Characteristics
3. ADT Operations

21_GRAPHS 2
Graphs
Represents relationships!
Data is stored in vertices
Relationships are represented by edges
Formal definition:
◦ G = {V,E}
◦ A graph is a set of vertices (nodes) and
◦ A set of edges that connect the vertices

21_GRAPHS 3
Graph
Examples
The internets!

computer-science-
schools.com/university-
of-nevada-reno

unr.edu

nevadawolfpack.com unr.edu/students

21_GRAPHS
Graph Examples
Social Networks

21_GRAPHS 5
Types of Graphs
Categorized by relationships (or
edges)
Connected
◦ Each vertex has at least one edge
Disconnected
◦ At least one vertex does not have
an edge
Complete
◦ Each vertex has an edge to all
other vertices

21_GRAPHS 6
Types of Graphs
Categorized by relationships (or edges)
Orientation (Direction)
◦ Undirected
◦ Edges simply connect vertices
◦ Directed
◦ Edges have a start (head) and
finish (tail) computer-
science-
◦ Represented as ordered pairs schools.com/un
iversity-of-
nevada-reno

Weighted
◦ Each edge has an associated weight
or cost
unr.edu

nevadawolfpac unr.edu/student
k.com s

21_GRAPHS 7
Graph Characteristics
 
Adjacent
◦ Nodes have an edge connecting them
◦ v is adjacent to u if
Path
◦ Sequence of edges which joins a
sequence of vertices
◦ Represented as a list of vertices such
that for all
Cycle
◦ A path that begins and end at the
same node
8

21_GRAPHS
Graph Characteristics
Path Length
◦ Number of edges in path
Path Cost
◦ For weighted graphs
◦ Sum of the costs of each edge

21_GRAPHS
Graph ADT
Operations
• getNumVertices(): int
• getNumEdges(): int
• add(start: LabelType, end: LabelType, edgeWeight: int):
boolean
• remove(start: LabelType, end: LabelType): boolean
• getEdgeWeight(start: LabelType, end: LabelType): int
• depthFirstTraversal(start: LabelType, visit): void
• breadthFirstTraversal(start: LabelType, visit): void

7_STACKS 10
Graph ADT
Operations
• add(start: LabelType, end: LabelType, edgeWeight: int):
boolean
• Creates an undirected edge in this graph between two vertices
that have the given labels. If such vertices do not exist,
creates them and adds them to the graph before creating the
edge

7_STACKS 11
Graph ADT
Operations
• remove(start: LabelType, end: LabelType): boolean
• Removes an edge from this graph. If a vertex is left with no
other edges, it is removed from the graph since this is a
connected graph.

7_STACKS 12
Graph Storage
Adjacency Matrix
• 2D array
• Each element represents the edge between two vertices
• Can represent directed, undirected, and weighted graphs

7_STACKS 13
Directed Graph Matrix
P Q R S T W X Y Z
0 1 2 3 4 5 6 7 8
P 0 0 0 1 0 0 1 0 0 0
Q 1 0 0 0 0 0 0 1 0 0
R 2 0 0 0 0 0 0 1 0 0
S 3 0 0 0 0 1 0 0 0 0
T 4 0 0 0 0 0 1 0 0 0
W 5 0 0 0 1 0 0 0 1 0
X 6 0 0 0 0 0 0 0 0 0
Y 7 0 0 1 0 0 0 0 0 1
Z 8 0 0 0 0 0 0 0 0 0

21_GRAPHS 14
Undirected Weighted Graph
Matrix
A B C D
0 1 2 3
A 0 ∞ 8 ∞ 6
B 1 8 ∞ 9 ∞
C 2 ∞ 9 ∞ ∞
D 3 6 ∞ ∞ ∞

21_GRAPHS 15
Graph Storage
Adjacency List
• 1D array of pointers
• Each element points to a List of vertices
• Can represent directed, undirected, and weighted graphs

7_STACKS 16
Undirected Weighted Graph
List
P 0 R W
Q 1 X
R 2 X
S 3 T
T 4 W
W 5 S Y
X 6
Y 7 R Z
Z 8

21_GRAPHS 17
Undirected Weighted Graph
List
A 0 B 8 D 6
B 1 A 8 C 9
C 2 B 9
D 3 A 6

21_GRAPHS 18
Graph Search
ADJACENCY MATRIX ADJACENCY LIST

• Supports seeing if there • Supports finding vertices


exists an edge from one adjacent to given vertex
vertex to another • Often requires less space
than adjacency matrix

7_STACKS 19
Graph Search
• AKA Traversals
• Visits all vertices it can reach
• Visits all vertices if and only if the graph is connected

7_STACKS 20
Graph Search
DEPTH FIRST SEARCH BREADTH FIRST SEARCH

• Goes as far as possible • Visits all vertices adjacent


from a vertex before to a vertex before going
backing up forward
• Last visited, first explored • First visited, first explored
• Stack! • Queue!

7_STACKS 21
Depth First Search
adjacency list stack
seen
0 0 a b f i

0 1 b a c e

0 2 c b d e

0 3 d c g h

0 4 e b c g

0 5 f a g

0 6 g d e f

0 7 h d
0 8 i a

22
Depth First Search
adjacency list stack
seen
0 0 a b f i

0 1 b a c e

0 2 c b d e

0 3 d c g h

0 4 e b c g

0 5 f a g

0 6 g d e f

0 7 h d
0 8 i a
a

23
Depth First Search
adjacency list stack
seen
1 0 a b f i

0 1 b a c e

0 2 c b d e

0 3 d c g h

0 4 e b c g

0 5 f a g

0 6 g d e f

0 7 h d
b
0 8 i a a

24
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

0 2 c b d e

0 3 d c g h

0 4 e b c g

0 5 f a g

0 6 g d e f
c
0 7 h d b
0 8 i a a

25
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

0 3 d c g h

0 4 e b c g

0 5 f a g
d
0 6 g d e f c
0 7 h d b
0 8 i a a

26
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

0 4 e b c g
g
0 5 f a g d
0 6 g d e f c
0 7 h d b
0 8 i a a

27
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h e
0 4 e b c g g
0 5 f a g d
1 6 g d e f c
0 7 h d b
0 8 i a a

28
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g
g
0 5 f a g d
1 6 g d e f c
0 7 h d b
0 8 i a a

29
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h f
1 4 e b c g g
0 5 f a g d
1 6 g d e f c
0 7 h d b
0 8 i a a

30
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g
g
1 5 f a g d
1 6 g d e f c
0 7 h d b
0 8 i a a

31
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g

1 5 f a g
d
1 6 g d e f c
0 7 h d b
0 8 i a a

32
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g
h
1 5 f a g d
1 6 g d e f c
0 7 h d b
0 8 i a a

33
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g

1 5 f a g
d
1 6 g d e f c
1 7 h d b
0 8 i a a

34
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g

1 5 f a g

1 6 g d e f
c
1 7 h d b
0 8 i a a

35
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g

1 5 f a g

1 6 g d e f

1 7 h d
b
0 8 i a a

36
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g

1 5 f a g

1 6 g d e f

1 7 h d
0 8 i a
a

37
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g

1 5 f a g

1 6 g d e f

1 7 h d
i
0 8 i a a

38
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g

1 5 f a g

1 6 g d e f

1 7 h d
1 8 i a
a

39
Depth First Search
adjacency list stack
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g

1 5 f a g

1 6 g d e f

1 7 h d
1 8 i a

40
Breadth First Search
adjacency list queue
seen
0 0 a b f i

0 1 b a c e

0 2 c b d e

0 3 d c g h

0 4 e b c g

0 5 f a g

0 6 g d e f

0 7 h d
0 8 i a

41
Breadth First Search
adjacency list queue
seen
1 0 a b f i a
0 1 b a c e

0 2 c b d e

0 3 d c g h

0 4 e b c g

0 5 f a g

0 6 g d e f

0 7 h d
0 8 i a

42
Breadth First Search
adjacency list queue
seen
1 0 a b f i b
1 1 b a c e f
0 2 c b d e i

0 3 d c g h

0 4 e b c g

1 5 f a g

0 6 g d e f

0 7 h d
1 8 i a

43
Breadth First Search
adjacency list queue
seen
1 0 a b f i f
1 1 b a c e i
1 2 c b d e c
0 3 d c g h
e

1 4 e b c g

1 5 f a g

0 6 g d e f

0 7 h d
1 8 i a

44
Breadth First Search
adjacency list queue
seen
1 0 a b f i i
1 1 b a c e c
1 2 c b d e e
0 3 d c g h
g

1 4 e b c g

1 5 f a g

1 6 g d e f

0 7 h d
1 8 i a

45
Breadth First Search
adjacency list queue
seen
1 0 a b f i c
1 1 b a c e e
1 2 c b d e g

0 3 d c g h

1 4 e b c g

1 5 f a g

1 6 g d e f

0 7 h d
1 8 i a

46
Breadth First Search
adjacency list queue
seen
1 0 a b f i e
1 1 b a c e g
1 2 c b d e d

1 3 d c g h

1 4 e b c g

1 5 f a g

1 6 g d e f

0 7 h d
1 8 i a

47
Breadth First Search
adjacency list queue
seen
1 0 a b f i g
1 1 b a c e d
1 2 c b d e

1 3 d c g h

1 4 e b c g

1 5 f a g

1 6 g d e f

0 7 h d
1 8 i a

48
Breadth First Search
adjacency list queue
seen
1 0 a b f i d
1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g

1 5 f a g

1 6 g d e f

0 7 h d
1 8 i a

49
Breadth First Search
adjacency list queue
seen
1 0 a b f i h
1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g

1 5 f a g

1 6 g d e f

1 7 h d
1 8 i a

50
Breadth First Search
adjacency list queue
seen
1 0 a b f i

1 1 b a c e

1 2 c b d e

1 3 d c g h

1 4 e b c g

1 5 f a g

1 6 g d e f

1 7 h d
1 8 i a

51
Next Class
Graphs
Textbook:
• Chapters 20
Internet:
•https://www.autonomousrobotslab.com/uploads/5/8
/4/4/58449511/cs302-131-graphs-introduction.pdf

21_GRAPHS 52

You might also like