ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… ·...
Transcript of ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… ·...
![Page 1: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/1.jpg)
ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs
Chris Wyatt Electrical and Computer Engineering Virginia Tech
![Page 2: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/2.jpg)
Graphs
One can approach graphs from different perspectives 1) It is a data structure: extension of trees 2) Is is a mathematical construct 3) A model of many different real world problems
![Page 3: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/3.jpg)
From trees to graphs
Tree Graph
![Page 4: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/4.jpg)
Definition of a graph
A graph is a collection of vertices (nodes), V, and a set of pairs of vertices, E. G = {V,E}
Example: V = {a b c d} E = {(a,c) (c,b) (b,d)
a b c d
a b c d
a and c are
adjacent
![Page 5: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/5.jpg)
Terminology: Edges
Edges may be undirected or directed
order of vertex pairs neglected
order of vertex pairs determines direction
![Page 6: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/6.jpg)
Terminology: graphs vs multi-graphs
Graphs have at most one edge between two vertices V = {a b c d} E = {(a,c) (c,b) (b,d)} Multigraphs allow duplicate edges V = {a b c d} E = { (a,c) (c,b) (b,d) (a,c) }
a b c d
a b c d
![Page 7: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/7.jpg)
Terminology: subgraphs
Any subset of V and E forms a subgraph
a
b
c
f
i
a
b
i
Undirected Graph G A subgraph of G
![Page 8: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/8.jpg)
Terminology: subgraphs
Any subset of V and E forms a subgraph
a
b
c
f
i
Undirected Graph G Another subgraph of G
b
c
f
![Page 9: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/9.jpg)
Terminology: paths
A path is a sequence of vertices connected by edges
a
b c d
e
f g
h
i
j
k
l
Paths may be directed or undirected
![Page 10: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/10.jpg)
Terminology: paths
A path is a sequence of vertices connected by edges
a
b c d
e
f g
h
i
j
k
l
Paths may be directed or undirected
![Page 11: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/11.jpg)
Terminology: cycle
A cycle is a path that starts and stops at the same vertex
a
b c d
e
f g
h
i
j
k
l
![Page 12: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/12.jpg)
Terminology: cycle
A cycle is a path that starts and stops at the same vertex
a
b c d
e
f g
h
i
j
k
l
![Page 13: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/13.jpg)
Terminology: cycle
A cycle is a path that starts and stops at the same vertex
a
b c d
e
f g
h
i
j
k
l
![Page 14: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/14.jpg)
Terminology: connected / disconnected
A graph is connected if every pair of vertices are connected by at least one path
a
b
c
d
f
g
i k
l
![Page 15: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/15.jpg)
Terminology: connected / disconnected
Otherwise it is disconnected.
a
b
c
d
f
g
i k
l
![Page 16: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/16.jpg)
Vertices and Edges can have properties or attributes attached to them.
Example: driving routes between cities
Blacksburg
Radford
Christiansburg
Roanoke
7.86 miles 15 min
14.3 miles 23 min 10 miles
17 min 46.7 miles 53 min
37.5 miles 40 min
![Page 17: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/17.jpg)
Vertices and Edges can have properties or attributes attached to them.
Example: driving routes between cities
Blacksburg
Christiansburg
7.86 miles 15 min
Name GPS coord Population
etc length in miles driving time road type (2 lane, 4 lane, access controlled)
![Page 18: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/18.jpg)
A commonly encountered graph is one where the edge property is a weight.
Weighted graphs (directed or undirected) have edges whose property is a cost.
Often one want to find paths connecting nodes where
some function of the sum of the weights is optimal a min or max).
For example: what is the shortest route from
Roanoke to Radford? what is the fastest? etc.
![Page 19: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/19.jpg)
Some categories of graphs
Complete Random Planar
![Page 20: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/20.jpg)
Small-World Graphs
Small world graphs often appear in the real world and have very interesting properties.
• Seven degrees of Kevin Bacon • Large Scale Computer Networks
• Brains Neural Connections in the worm C-elegans
279 vertices
6,417 edges
![Page 21: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/21.jpg)
Example uses of graphs
Path planning Layout routing Games and puzzles Many kinds of circuits Networked systems Optimization Constraint Satisfaction Logical Inference Probabilistic Inference ..... on and on .....
![Page 22: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/22.jpg)
Implementing Graphs
There is no graph data structure in the current standard C++ library.
It is easy to roll your own using existing standard
library containers. There is also the boost graph library (www.boost.org)
![Page 23: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/23.jpg)
Three common approaches to representing graphs.
Adjacency matrix: given N vertices, the edges are indicated by an NxN matrix
Adjacency List: given N vertices, the edges are
indicated by a list of connected vertices for each vertex.
Pointer based: given a pointer to a vertex, which
contains pointers to it’s adjacent vertices
![Page 24: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/24.jpg)
Graph using an adjacency matrix
a b c d
a 0 1 0 1
b 1 0 0 0
c 0 1 0 1
d 0 0 0 0
a
b
c
d
- Undirected graphs have a symmetric matrix - Weighted graphs have integer or real entries
![Page 25: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/25.jpg)
Graph using an adjacency list
a
b
c
d
a
b
c
d
b d
a
b d
- the lists could be vectors, linked, or trees
![Page 26: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/26.jpg)
Graph using pointers
- graph must have a root and be connected. - Why?
a
b
c
d
root
![Page 27: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/27.jpg)
Advantages/Disadvantages of implementations
Adjacency matrix Advantages
1. simple 2. space efficient for dense graphs (~ complete) 3. fast access to all edges
Disadvantages 1. space inefficient for sparse graphs
![Page 28: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/28.jpg)
Advantages/Disadvantages of implementations
Adjacency list Advantages
1. space efficient for sparse graphs Disadvantages
1. space inefficient for dense graphs 2. access to arbitrary edges slower
![Page 29: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/29.jpg)
Advantages/Disadvantages of implementations
Pointer based Advantages
1. space efficient for sparse graphs Disadvantages
1. space inefficient for dense graphs 2. access to arbitrary edges slower 3. cannot represent disconnected graphs (easily)
![Page 30: ECE 2574 Introduction to Data Structures and Algorithms 38 ...ECE2574/meeting/38-graphs/slid… · ECE 2574 Introduction to Data Structures and Algorithms 38: Introduction to Graphs](https://reader035.fdocuments.net/reader035/viewer/2022070717/5edc7720ad6a402d666721f2/html5/thumbnails/30.jpg)
Next Actions and Reminders
Read CH pp. 614-630 on graph traversals and algorithms
Program 5 is due 12/11
Please Fill out the SPOT survey!