CMPS 2433 CHAPTER 4 - PART 2 GRAPHS Midwestern State University

advertisement
CMPS 2433
CHAPTER 4 - PART 2
GRAPHS
Midwestern State University
Department of Computer Science
Dr. Ranette Halverson
1
4.3 Shortest Paths & Distance
•Traversal: process visiting every vertex
•Tree or Graph
•Is a traversal necessary to solve a particular
problem?
•Breadth-first vs Depth-first searches &
traversals
•Distance between 2 vertices: number of
edges on the shortest path between 2
vertices (may be more than one path)
2
Shortest Paths & Distance
•Traversal
•Distance
•Breadth-first
•Depth-first
3
Finding Distances – Breadth-first
•PROBLEM: Find distance between vertex A
and all other vertices in the graph
•Notation: Vertex label 4(F) means distance
from A to current vertex is distance 4 and
predecessor node on the path is F
•Let A be vertex 1
•Label on vertex 4 is
2(5)
4
Finding Distances – Breadth-first
•PROBLEM: Find distance between vertex A
and all other vertices in the graph
•Choose start vertex 1 – label 0(-)
•Label all vertices adjacent to vertex 1
•Repeat for newly labeled vertices (never
change label on vertex)
•Continue until all vertices
are labeled.
5
Breadth-First Search Algorithm
•Page 183
•Complexity
•Process each vertex & edge once  v + e ops
• Remember: e <= C(v,2) = (v (v-1))/2 - - Why?
•v + (v (v-1))/2  O(v2)
6
Weighted Graphs
•Graph in which a number = weight is
assigned to each edge
•Weight of Path: sum of edges in path
•Used in MANY applications
•Distance, Cost, Size
•Shortest Path Problems
7
Dijkstra’s Algorithm
•Finds shortest path between a
designated vertex S & all other vertices in
graph & provides actual shortest paths
•Overview:
•Start at S
•Find nodes adjacent
•Each step update with shorter path, if
available
8
Dijkstra’s Algorithm - Overview
3
S
A
5
10
1
B
4
C
9
Number of Paths
Theorem 4.7 (unweighted graph)
For a graph G with vertices V1, V2,…Vn &
adjacency matrix A, the number of paths of
length m from Vi to Vj is the (i, j) entry of Am.
See example on page 189-190
Review Matrix Multiplication!!
10
Flubber Graphs
•Named after a professor who developed
in 1960’s
•Not in our book
•Flubber Graph: A graph (set of vertices &
edges) in which each vertex is
flubberized.
11
Homework
•Section 4.3 – pages 190-193
Exercises – All 1 – 16, 20
1 – 4, Breadth-first algorithm,
5 – 8, Dijkstra’s Algorithm
9 – 12, modified Dijkstra’s,
13 – 16, Theorem 4.7
12
Graph Definitions
•Flubber Graph
•A graph in which
each vertex is
flubberized
•Connected Graph
•A graph in which
each vertex is
connected
13
Graph Definitions
•Flubber Graph
•Connected Graph
•A graph in which
•A graph in which
each vertex is
each vertex is
flubberized
connected
•A graph in which
•A graph in which
each vertex is
each vertex is
flubberized with
connected to
every other vertex
every other vertex
14
4.4 Coloring a Graph
•Omit section but define problem
15
4.5 Directed Graphs - Multigraphs
•Directed Graph: finite, non-empty set V and
E (vertices & edges) in which each edge has
specified direction between vertices
•Directed Edge: An edge (A,B) is from vertex
A to B, but not vice versa
•Degree
•In degree: # of edges directed into a vertex
•Out degree: # of edges directed from a vertex
16
Representation of Directed Graphs
•Adjacency Matrix: works but matrix is not
symmetrical
•Adjacency List: also works
•Directed Graph with weighted edges
•Must also store the weights
•Multigraph
•Parallel Directed Edges: Represent?
•Loop: Represent?
17
Paths in Directed Graphs
•Directed Path: sequence of edges from
vertex A to B, following edge direction
•Length of Path: number of edges on path
•Simple Directed Path: No repeated vertices
•Directed cycle
•Strongly Connected: A directed multigraph
which contains a path from vertex A to B for
every pair of vertices
•Means A to B & B to A
18
Homework
•Section 4.5 – Pages 212 – 218
•Exercises 1 – 22, 24, 25, 35-37, 74,77
•Maybe others later – or just try some
others
Omit from study pp. 208-212
19
Download