Upload
eric-price
View
228
Download
1
Tags:
Embed Size (px)
Citation preview
CSIT 402 Graph Introduction 2
Graphs
• Graphs are composed of› Nodes (vertices)
• Can be labeled
› Edges (arcs)• Directed (arrowhead)• Undirected• Can be labeled
node
edge
CSIT 402 Graph Introduction 3
Graphs: Data Structure Progression
Consider previous data structures•Linked List: Nodes with 1 incoming edge and 1 outgoing edge
•Binary Trees/Heaps: Nodes with 1 incoming edge and 2 outgoing edges
•General Trees: Nodes with 1 incoming edge and any number of outgoing edges
Idea: All of the above are graphsMore examples on next seven slides…
10
96 99
94
97
Value Nextnode
Value Nextnode
CSIT 402 Graph Introduction 4
Course Prerequisites
321143
142
322
326
341370
378
401
421Nodes = coursesDirected edge = prerequisite
373
410
413
415
417
461
CSIT 402 Graph Introduction 5
Representing a Maze
S
Nodes = locationsEdge = paths between locations
S
E
B
E
Directed or Undirected?
CSIT 402 Graph Introduction 6
Representing Electrical Circuits
Nodes = battery, switch, resistor, etc.Edges = connections
Battery Switch
Resistor
Directed or Undirected?
CSIT 402 Graph Introduction 7
Program statements
x1=q+y*zx2=y*z-q
Naive:
commonsubexpression
eliminated:
y z
*
-
q
+
q *
x1 x2
y z
-
q
+
q *
x1 x2
Nodes = symbols/operatorsEdges = relationships
y*z calculated twice
Directed or Undirected?
CSIT 402 Graph Introduction 8
PrecedenceS1 a=0;
S2 b=1;
S3 c=a+1
S4 d=b+a;
S5 e=d+1;
S6 e=c+d;
3
1 2
6
5
4Which statements must execute before S6?
S1, S2, S3, S4
Nodes = statementsEdges = precedence requirements
Directed or Undirected?
Values
CSIT 402 Graph Introduction 9
Information Transmission in a Computer Network
Seattle
New York
L.A.
Tokyo
Sydney
Seoul
Nodes = computersEdges = transmission rates
128
140
18130
16
56Directed or Undirected?
Values
CSIT 402 Graph Introduction 10
Traffic Flow on Highways
Nodes = citiesEdges = # vehicles on connecting highway
UW
Directed or Undirected?
CSIT 402 Graph Introduction 11
Formal Definition
• A graph G is a tuple (V, E) where› V is a set of vertices or nodes › E is a set of edges
• An Edge e is a pair (v1,v2), where vi ε V› Example:
• V = {A, B, C, D, E, F}• E = {(A,B), (A,D), (B,C), (C,D), (C,E), (D,E)}
A
BC
ED
F
• If the order of edge pairs (vi, vj) is important (e.g prerequisites), the graph is directed (also called a digraph): (vi, vj) (vj, vi) , i j
• If the order of edge pairs (vi, vj) is unimportant (e.g. maze), the graph is called an undirected graph (never called an undigraph): In this case, (vi, vj) = (vj, vi) , i j
CSIT 402 Graph Introduction 12
Directed vs Undirected Graphs
vivj
vivj
CSIT 402 Graph Introduction 13
Undirected Graph Terminology
• Two vertices u and v are adjacent in an undirected graph G if {u,v} is an edge in G› edge e = {u,v} is incident (in contact) with vertex u and vertex v
• The degree of a vertex in an undirected graph is the number of edges incident with it› a self-loop counts twice (both ends count)
› denoted with deg(v)
A
BC
ED
F
Deg(D) = 3 Deg(F) = 0
(A,B) is incidentto A and to B
Self-loop
B is adjacent to C and C is adjacent to B
CSIT 402 Graph Introduction 14
Directed Graph Terminology
• Vertex u is adjacent to vertex v in a directed graph G if (u,v) is an edge in G› vertex u is the initial vertex of (u,v)
• Vertex v is adjacent from vertex u› vertex v is the terminal (or end) vertex of (u,v)
• Degree› in-degree is the number of edges with the vertex
as the terminal vertex› out-degree is the number of edges with the vertex
as the initial vertex
CSIT 402 Graph Introduction 15
Directed Graph Terminology
A
BC
ED
F
In-degree = 2Out-degree = 1
In-degree = 0Out-degree = 0
B adjacent to C and C adjacent from B
CSIT 402 Graph Introduction 16
Handshaking Theorem
• Let G=(V,E) be an undirected graph with |E|=e edges. Then
• Every edge contributes +1 to the degree of each of the two vertices it is incident with › number of edges is exactly half the sum of deg(v)› the sum of the deg(v) values must be even
Vv
deg(v)2e Add up the degrees of all vertices.
CSIT 402 Graph Introduction 17
• Space and time are analyzed in terms of:
• Number of vertices = |V| and
• Number of edges = |E|
• There are at least two ways of representing graphs:
• The adjacency matrix representation
• The adjacency list representation
Graph Representations
CSIT 402 Graph Introduction 18
A B C D E F
0 1 0 1 0 0 1 0 1 0 0 0 0 1 0 1 1 0 1 0 1 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0
M(v, w) = 1 if (v, w) is in E
0 otherwise
A
B
C
D
E
F
Space = |V|2
A
BC
EDF
Adjacency Matrix
CSIT 402 Graph Introduction 19
A B C D E F
0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
A
B
C
D
E
F
Space = |V|2
M(v, w) = 1 if (v, w) is in E
0 otherwise
A
BC
EDF
Adjacency Matrix for a Digraph
CSIT 402 Graph Introduction 20
B D
B D
C
A C E
D
E
A C
A
B
C
D
E
F
A
BC
EDF
Space = |V| + 2 |E|
For each v in V, L(v) = list of w such that (v, w) is in E a b
Adjacency List
list ofneighbors
CSIT 402 Graph Introduction 21
B D
E
D
C
a b
A
B
C
D
E
F
E
A
BC
EDF
For each v in V, L(v) = list of w such that (v, w) is in E
Space = |V| + |E|
Adjacency List for a Digraph
CSIT 402 Directed Graphs
22
Paths and Cycles
• Given a directed graph G = (V,E), a path is a sequence of vertices v1,v2, …,vk such that:
› (vi,vi+1) in E for 1 < i < k• path length = number of edges in the path• path cost = sum of costs of each edge (if edges have costs)
• A graph contains a cycle if, there exists a path v1,v2, …,vk, k > 1 such that vk = v1
• G is acyclic if it has no cycles.