![Page 1: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/1.jpg)
COSC 2007Data Structures II
Chapter 14Graphs I
![Page 2: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/2.jpg)
2
Topics
Introduction & Terminology ADT Graph
![Page 3: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/3.jpg)
3
Introduction Graphs
Important mathematical concept that have significant application in computer science
Can be viewed as a data structure or ADT Provide a way to represent relationships between data
Questions Answered by Using Graphs: Airline flight scheduling:
What is the shortest distance between two cities?
![Page 4: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/4.jpg)
4
• A graph G is a pair (V, E) where
V is a set of vertices (nodes) V = {A, B, C, D, E, F}
E is a set of edges (connect vertex) E = {(A,B), (A,D), (B,C),(C,D), (C,E), (D,E)}
A
BC
F
D E
Terminology and Notations
![Page 5: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/5.jpg)
5
• If edge pairs are ordered, the graph is directed, otherwise undirected.
• We draw edges in undirected graphs with lines with no arrow heads.
This is an undirected graph.
(B, C) and (C, B) mean the same edge
A
BC
F
D E
Terminology and Notations
![Page 6: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/6.jpg)
6
• If edge pairs are ordered, the graph is directed, otherwise undirected.
• We draw edges in directed graphs with lines with arrow heads.
A
BC
F
D E
This is adirected graph.
This edge is (B, C).
(C, B) would mean a directed edge from C to B
Terminology and Notations
![Page 7: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/7.jpg)
7
• Directed Graph (Digraph):If an edge between two nodes has a direction (directed edges)
Out-degree (OD) of a Node in a Digraph:The number of edges exiting from the node
In-degree (ID) of a Node in a Digraph:The number of edges entering the node
A
BC
F
D E
This is adirected graph.
Terminology and Notations
![Page 8: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/8.jpg)
8
• Vertex w is adjacent to v if and only if (v, w) E.
• In a directed graph the order matters:
B is adjacent to A in this graph, but A is not adjacent to B.
A
BC
F
D E
Terminology and Notations
![Page 9: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/9.jpg)
9
• Vertex w is adjacent to v if and only if (v, w) E.
• In an undirected graph the order does not matter:
we say B is adjacent to A and that A is adjacent to B.
A
BC
F
D E
Terminology and Notations
![Page 10: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/10.jpg)
10
• In some cases each edge has a weight (or cost) associated with it.
• The costs might be determined by a cost function
E.g., c(A, B) = 3,c(D,E) = – 2.3, etc.
4
A
BC
F
D E
3
7.5
– 2.3
1.24.5
Terminology and Notations
![Page 11: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/11.jpg)
11
• In some cases each edge has a weight (or cost) associated with it.
• When no edge exists between two vertices, we say the cost is infinite.
E.g., c(C,F) =
A
BC
F
D E
34
7.5
– 2.3
1.24.5
Terminology and Notations
![Page 12: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/12.jpg)
12
• Let G = (V, E) be a graph.
A subgraph of G is a graph H = (V*, E*) such that V* V and E* E.
A
BC
F
D E
E.g.,
V* = {A, C, D},
E* = {(C, D)}.
Terminology and Notations
![Page 13: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/13.jpg)
13
• Let G = (V, E) be a graph.
A subgraph of G is a graph H = (V*, E*) such that V* V and E* E.
AC
D
E.g.,
V* = {A, C, D},
E* = {(C, D)}.
Terminology and Notations
![Page 14: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/14.jpg)
14
• Let G = (V, E) be a graph.
A path in the graph is a sequence of vertices
w , w , . . . , w such that (w , w ) E for 1<= i <= N–1. 1 2 N i i+1
A
BC
F
D E
E.g., A, B, C, Eis a path inthis graph
Terminology and Notations
![Page 15: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/15.jpg)
15
• Let w , w , . . . , w be a path.
The length of the path is the number of edges, N–1, one less than the number of vertices in the path.
A
BC
F
D E
E.g., the length ofpath A, B, C, Eis 3.
1 2 N
Terminology and Notations
![Page 16: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/16.jpg)
16
• Let w , w , . . . , w be a path in a directed graph.
Since each edge (w , w ) in the path is ordered,
the arrows on the path are always directed along
the path.
A
BC
F
D E
1 2 N
i i+1
E.g., A, B, C, Eis a path in this directed graph, but . . .
Terminology and Notations
. . . but A, B, C, Dis not a path, since (C, D) is not an edge.
![Page 17: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/17.jpg)
17
A path is simple if all vertices in it are distinct, except that the first and last could be the same.
A
BC
F
D E
E.g., thepath A, B, C, Eis simple . . .
Terminology and Notations
. . . and so is thepath A, B, C, E, D, A.
![Page 18: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/18.jpg)
18
If G is an undirected graph, we say it is connected if there is a PATH from every vertex to every other vertex.
A
BC
F
D E
This undirected graph is not connected.
Terminology and Notations
![Page 19: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/19.jpg)
19
If G is an directed graph, we say it is strongly connected if there is a path from every vertex to every other vertex.
This directed graph is strongly connected.
A
BC
F
D E
Terminology and Notations
![Page 20: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/20.jpg)
20
If G is an directed graph, we say it is strongly connected if there is a path from every vertex to every other vertex.
This directed graph is not strongly connected; e.g., there’s no path
from D to A.
A
BC
F
D E
Terminology and Notations
![Page 21: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/21.jpg)
21
If G is directed and not strongly connected, but the underlying graph (without direction to the edges) is connected, we say that G is weakly connected.
This directed graph is not strongly connected, but it isweakly connected, since . . .
A
BC
F
D E
Terminology and Notations
![Page 22: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/22.jpg)
22
If G is directed and not strongly connected, but the underlying graph (without direction to the edges) is connected, we say that G is weakly connected.
. . . since theunderlying undirected
graph is connected.
A
BC
F
D E
Terminology and Notations
![Page 23: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/23.jpg)
23
Cycle: a path that begins and ends at the
same node but doesn't pass through other
nodes more than once
A
BC
F
D E
The pathA, B, C, E, D, A
is a cycle.
Terminology and Notations
![Page 24: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/24.jpg)
24
•A graph with no cycles is called acyclic.
A
BC
F
D E
This graph is acyclic.
Terminology and Notations
![Page 25: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/25.jpg)
25
•A graph with no cycles is called acyclic.
This directed graph is not acyclic, . . .
A
BC
F
D E
Terminology and Notations
![Page 26: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/26.jpg)
26
•A graph with no cycles is called acyclic.
. . . but this one is.A Directed Acyclic Graph is often called simply a DAG.
A
BC
F
D E
Terminology and Notations
![Page 27: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/27.jpg)
27
•A complete graph is one that has an edge between every pair of vertices.
A
BC
D E
Incomplete:
Terminology and Notations
![Page 28: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/28.jpg)
28
•A complete graph is one that has an edge between every pair of vertices. (if the graph contains the maximum possible number of edges) A complete graph is also connected, but the converse is not true
Complete:
A
BC
D E
Terminology and Notations
![Page 29: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/29.jpg)
29
•A complete graph is one that has an edge between every pair of vertices.
• Suppose G = (V, E) is complete. Can you express |E| as a function of |V|?Complete:
A
BC
D E
This graph has |V| = 5 vertices and |E| = 10 edges.
Terminology and Notations
![Page 30: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/30.jpg)
30
•A free tree is a connected, acyclic, undirected graph.
•“Free” refers to the fact that there is no vertex designated as the “root.”
A
BC
D E
F
Terminology and Notations
This is a free tree.
![Page 31: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/31.jpg)
31
•A free tree is a connected, acyclic, undirected graph.
•If some vertex is designated as the root, we have a rooted tree.
A
BC
D E
F
root
Terminology and Notations
![Page 32: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/32.jpg)
32
•If an undirected graph is acyclic but possibly disconnected, it is a forest.
A
BC
D E
F
GH
I
JL
K
This is a forest. It contains three free trees.
Terminology and Notations
![Page 33: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/33.jpg)
33
•If an undirected graph is acyclic but possibly disconnected, it is a forest.This graph contains a cycle. Therefore it is neither a free tree nor a forest.
A
BC
D E
F
GH
I
JL
K
Terminology and Notations
![Page 34: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/34.jpg)
34
Review In a graph, a vertex is also known as a(n)
______. node edge path cycle
![Page 35: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/35.jpg)
35
Review A graph consists of ______ sets.
two three four five
![Page 36: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/36.jpg)
36
Review A subset of a graph’s vertices and edges
is known as a ______. bar graph line graph Subgraph circuit
![Page 37: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/37.jpg)
37
Review Two vertices that are joined by an edge
are said to be ______ each other. related to bordering utilizing adjacent to
![Page 38: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/38.jpg)
38
Review All ______ begin and end at the same
vertex and do not pass through any other vertices more than once. paths simple paths cycles simple cycles
![Page 39: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/39.jpg)
39
Review Which of the following is true about a
simple cycle? it can pass through a vertex more than once it can not pass through a vertex more than
once it begins at one vertex and ends at another it passes through only one vertex
![Page 40: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/40.jpg)
40
Review A graph is ______ if each pair of distinct
vertices has a path between them. complete disconnected connected full
![Page 41: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/41.jpg)
41
Review A complete graph has a(n) ______
between each pair of distinct vertices. edge path Cycle circuit
![Page 42: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/42.jpg)
42
Review The ______ of a weighted graph have
numeric labels. vertices edges paths cycles
![Page 43: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/43.jpg)
43
Review The edges in a ______ indicate a
direction. graph multigraph digraph spanning tree
![Page 44: COSC 2007 Data Structures II Chapter 14 Graphs I](https://reader035.vdocuments.site/reader035/viewer/2022062515/56649f515503460f94c754b6/html5/thumbnails/44.jpg)
44
Review If there is a directed edge from vertex x to
vertex y, which of the following can be concluded about x and y? y is a predecessor of x x is a successor of y x is adjacent to y y is adjacent to x