Lect 4 Graphs&GreedyAlgo
Lect 4 Graphs&GreedyAlgo
Lect 4 Graphs&GreedyAlgo
Computing
Lecture 4
Graphs and Greedy Algorithms (Draft)
1
Lecture Outline
◼ Graphs
◼ Graph Representation
◼ Greedy Strategy
2
Graphs
◼ A graph is a pictorial representation of a set of objects where
some pairs of objects are connected by links.
◼ The interconnected objects are represented by:
◼ points termed as vertices,
◼ links that connect the vertices are called edges.
◼ Graphs provide a way to represent relationships between data.
◼ A graph is a series of nodes connected with edges.
◼ Formally, a graph is a pair of sets (V, E), where V is the set of
vertices and E is the set of edges, connecting the pairs of
vertices.
◼ In this graph,
◼ V = {a, b, c, d, e}
◼ E = {ab, ac, bd, cd, de}
3
Terminology and Notations
◼ Graph(G = (V,E)):
◼ A set of objects called "Nodes (Vertices) (V)" and "Edges
(Arcs) (E)" that connect the vertices
◼ Node:
◼ A data element of the graph
◼ Edge
◼ A path between two nodes
◼ Adjacent nodes:
◼ If there is a direct edge that connects the nodes
◼ Neighbors of a Node:
◼ All nodes that are adjacent to it.
4
Graph Applications
◼ Graphs can model both real-world systems and abstract
problems, so they are used in hundreds of applications.
◼ Sampling of graphs applications:
1. Modeling connectivity in computer and communications networks.
2. Representing a map as a set of locations with distances between
locations; used to compute shortest routes between locations.
3. Modeling flow capacities in transportation networks.
4. Finding a path from a starting condition to a goal condition; for
example, in artificial intelligence problem solving.
5. Modeling computer algorithms, showing transitions from one
program state to another.
6. Finding an acceptable order for finishing subtasks in a complex
activity, such as constructing large buildings.
7. Modeling relationships such as family trees, business or military
organizations, and scientific taxonomies.
5
Graph Applications
◼ Many problems can be naturally formulated in terms of graphs
◼ Relationships between states or cities
◼ Printed circuit boards
◼ Departments in a corporation
◼ Switching stations in a power network
◼ Graph structure
◼ A simplification of hierarchical structures, which in turn, is a
simplification of a linear structure
◼ Has no notation of either a first or root node, or last or leaf
node.
6
Graph Applications
◼ Airline flight scheduling:
◼ What is the shortest distance between two airports?
◼ Planning time- schedules for investment projects
◼ What is the shortest time for a specific job could be
done?
◼ Testing integrated circuit boards:
◼ Are two points on a board connected?
7
Types of Graphs
◼ Connected
◼ All nodes are reachable from
all other nodes
◼ Disconnected
◼ One or more nodes are not
reachable from one or more
other nodes
Types of Graphs
◼ Directed Graph (Digraph):
◼ If an edge between two nodes has a direction.
◼ Tree is the special case of a directed graph that is connected
and has no cycles (Acyclic).
◼ Directed path:
◼ Is a sequence of directed edges between two nodes
◼ Strongly Connected Digraph:
◼ If for any two nodes I & J in the graph, there is a directed
path from I to J
◼ Weakly Connected Digraph:
◼ If for any two nodes I & J there is a directed path from I to J
or J to I (but not both)
9
Types of Edges 1
◼ Undirected
◼ An edge goes to and
A B
from a node
◼ An edge can be taken
both directions
◼ Edges show
connections
C ◼ Directed
◼ Edges can specifically
A B go to or from nodes
◼ Edges can still be
bidirectional
◼ Edges can show a one
◼ Un-weighted
◼ Edges have no values
◼ Edges just show some sort
of connection such as
possible paths, etc..
◼ Weighted
◼ Edges have a value attached
to them
◼ These values could be
distance, cost, time, etc...
Graph Representation
◼ Adjacency Matrix
◼ Adjacency Lists
▪ Linked Adjacency Lists
12
Graph Representation
◼ Adjacency Matrix
◼ 0/1 n x n matrix, where n = # of vertices
◼ A(i,j) = 1 iff (i,j) has an edge, otherwise 0
2
3
1 2 3 4 5
1 1 0 1 0 1 0
4 2 1 0 0 0 1
5 3 0 0 0 0 1
4 1 0 0 0 1
5 0 1 1 1 0
13
Adjacency Matrix Properties
15
Linked Adjacency Lists
Array Length = n
# of chain nodes = 2e (undirected graph)
# of chain nodes = e (digraph)
16
Array Adjacency Lists
◼ Each adjacency list is an array list.
2 aList[1] 2 4
3
[2] 1 5
1 [3] 5
[4] 5 1
4
5 aList[5] 2 4 3
Array Length = n
# of list elements = 2e (undirected graph)
# of list elements = e (digraph)
17
Graph Storage
◼ The storage requirements for the adjacency list depend on both
the number of edges and the number of vertices in the graph.
◼ There must be an array entry for each vertex (even if the vertex
is not adjacent to any other vertex and thus has no elements on
its linked list), and each edge must appear on one of the lists.
◼ Thus, the cost is O(V + E).
18
Sparse Matrices
• Sparse graph – relatively few edges
19
Unstructured Sparse Matrices
Airline flight matrix:
▪ airports are numbered 1 through n
▪ flight(i,j) = list of nonstop flights from airport i to airport j
▪ Let n = 1000
▪ n x n array of list references => 4 million bytes
Web analysis:
authority page … page that has many links to it
hub page … links to many authority pages
20
Representation of Unstr. Sparse Matrices
Single linear list in row-major order:
• scan the nonzero elements of the sparse matrix in row-
major order.
• each nonzero element is represented by a triple
• (row, column, value)
• the list of triples may be an array list or a linked list
(chain)
• Example
list =
0 0 3 0 4 row 1 1 2 2 4 4
0 0 5 7 0
0 0 0 0 0 column 3 5 3 4 2 3
0 2 6 0 0
value 3 4 5 7 2 6
21
Array Linear List Representation
row 1 1 2 2 4 4
list = column 3 5 3 4 2 3
value 3 4 5 7 2 6
element 0 1 2 3 4 5
row 1 1 2 2 4 4
column 3 5 3 4 2 3
value 3 4 5 7 2 6
22
Chain Representation
- Node structure:
row col
value next
1 3 1 5 2 3 2 4 4 2 4 3
3 4 5 7 2 6 null
firstNode
23
Array of Row Chains
Node structure
null
next
3 3 5 4
col value
null
3 5 4 7
0 0 3 0 4
0 0 5 7 0 null
0 0 0 0 0
0 2 6 0 0 null
2 2 3 6
row[]
24
Storage Complexity
25
Greedy Strategy
◼
Optimization problems
A greedy algorithm works in phases. At each phase:
◼ The best is taken, without regard for future consequences
◼ By choosing a local optimum at each step, we hope to end up
at a global optimum.
◼ A “greedy algorithm” sometimes works well for optimization
problems.
◼ An optimization problem is one in which we want to find, not just
a solution, but the best solution
◼ Kruskal’s algorithm for finding a minimum-cost spanning tree
◼ Always tries the lowest-cost remaining edge
◼ Prim’s algorithm for finding a minimum-cost spanning tree
◼ Always takes the lowest-cost edge between nodes in the
spanning tree and nodes not yet in the spanning tree
◼ Dijkstra’s algorithm for finding the shortest path in a graph
◼ Always takes the shortest edge connecting a known node to
an unknown node.
26
Shortest Path Problems
❑ Directed weighted graph.
27
Example
8 6
1
2 3
16
3 1
7 5
6 4 10
4
2 4 7
5 3
14
A path from 1 to 7.
8 6
1
2 3
3 1
16
6 7 4 5
10
4
2 4 7
5 3
14
8 6
1
2 3
3 1
16 7 5
6 4 10
4
2 4 7
5 3
14
31
Dijkstra's Algorithm
◼ Dijkstra's algorithm - is a solution to the single-source
shortest path problem in graph theory.
◼ Works on both directed and undirected graphs. However, all
edges must have nonnegative weights.
◼ Approach: Greedy
32
The author: Edsger Wybe Dijkstra
◼ May 11, 1930 – August 6, 2002
33
Dijkstra's Algorithm
◼ Dijkstra’s algorithm finds the shortest paths from a given node
to all other nodes in a graph
◼ Initially,
34
Dijkstra's algorithm - Pseudocode
35
Dijkstra Algorithm Example
36
Dijkstra Algorithm Example
37
Dijkstra Algorithm Example
38
Dijkstra Algorithm Example
39
Dijkstra Algorithm Example
40
Dijkstra Algorithm Example
41
Dijkstra Algorithm Example
42
Dijkstra Algorithm Example
43
Dijkstra Algorithm Example
44
Dijkstra Algorithm Example
45
Applications of Dijkstra's Algorithm
- Traffic Information Systems are most prominent use
- Mapping (Map Quest, Google Maps)
- Routing Systems
46
Applications of Dijkstra's Algorithm
Prof. Lauren Meyers (Biology
Dept.) uses networks to model the
spread of infectious diseases and
design prevention and response
strategies.
Vertices represent individuals,
and edges their possible contacts.
It is useful to calculate how a
particular individual is connected
to others.
Knowing the shortest path
lengths to other individuals can be
a relevant indicator of the
potential of a particular individual
to infect others.
47
Single Source All Destinations
Need to generate up to n (n is number of vertices)
paths (including path from source to itself).
Greedy method:
▪ Construct these up to n paths in order of
increasing length.
▪ Assume edge costs (lengths) are >= 0.
▪ So, no path has length < 0.
▪ First shortest path is from the source vertex to
itself. The length of this path is 0.
48
Greedy Single Source All Destinations
8 6
1
2 3
3 1
16
6 7 4 5 10
4
2 4 7
5 3
14
Path Length 1 2 6
1 0
1 3 5 4 9
1 3 2
1 3 6 10
1 3 5 5 1 3 6 7 11 49
Greedy Single Source All Destinations
Path Length • Each path (other
1 0 than first) is a one
edge extension of a
1 3 2 previous path.
•Next shortest path
1 3 5 5 is the shortest one
6 edge extension of an
1 2
already generated
shortest path.
1 3 5 4 9
1 3 6 10
1 3 6 7 11
50
Greedy Single Source All Destinations
◼ Let d(i) (distanceFromSource(i)) be the length of a
shortest one edge extension of an already
generated shortest path, the one edge extension
ends at vertex i.
◼ The next shortest path is to an as yet unreached
vertex for which the d() value is least.
◼ Let p(i) (predecessor(i)) be the vertex just before
vertex i on the shortest one edge extension to i.
51
Greedy Single Source All Destinations
8 6
1
2 3
3 1
16
6 7 4 5 10
4
2 4 7
5 3
1 14
[1] [2] [3] [4] [5] [6] [7]
d 0 6 2 16 - - 14
p - 1 1 1 - - 1
52
Greedy Single Source All Destinations
8 6
1
2 3
3 1
16 7 5
6 4 10
4
2 4 7
5 3
1 14
1 3 [1] [2] [3] [4] [5] [6] [7]
d 0 6 2 16 55- 10- 14
p - 1 1 1 3- 3- 1
53
Greedy Single Source All Destinations
8 6
1
2 3
3 1
16
6 7 4 5 10
4
2 4 7
5 3
1 14
1 3 [1] [2] [3] [4] [5] [6] [7]
1 3 5 d 0 66 2 169 5- 10 - 14
p - 1 1 51 3- 3- 1
54
Greedy Single Source All Destinations
8 6
1
2 3
3 1
16
6 7 4 5
10
4
2 4 7
5 3
1 14
1 3 [1] [2] [3] [4] [5] [6] [7]
1 3 5 d 0 6 2 99 5- 10- 14
p - 1 1 5 3- 3- 1
1 2
55
Greedy Single Source All Destinations
8 6
1
2 3
3 1
16
6 7 4 5 10
4
2 4 7
5 3
1 14
1 3 [1] [2] [3] [4] [5] [6] [7]
1 3 5 d 0 6 2 9 5- 10 - 14
12
p - 1 1 5 3- 3- 1 4
1 2
1 3 5 4
56
Greedy Single Source All Destinations
8 6
1
2 3
3 1
16
6 7 4 5
10
4
2 4 7
5 3
14
1 3 6 [1] [2] [3] [4] [5] [6] [7]
d 0 6 2 9 5- 10- 11 12
1
p - 1 1 5 3- 3- 1 464
57
Greedy Single Source All Destinations
Path Length
1 0
1 3 2
1 3 5 5
[1] [2] [3] [4] [5] [6] [7]
1 2 6
0 6 2 9 5- 10
- 14
12
11
1 3 5 4 9 - 1 1 5 3- -
3 41
6
1 3 6 10
1 3 6 7 11
58
Time Complexity
◼ O(n) to select next destination vertex.
◼ O(out-degree) to update d() and p() values when
adjacency lists are used.
◼ O(n) to update d() and p() values when adjacency
matrix is used.
◼ Selection and update done once for each vertex to
which a shortest path is found.
◼ Total time is O(n2 + e) = O(n2).
59
Time Complexity
◼ When a min heap of d() values is used in place of
the linear list L of reachable vertices:
◼ Total time is O((n+e) log n), because O(n) remove
min operations and O(e) change key (d() value)
operations are done.
60
A spanning tree
◼ Determining the
location of roads
◼ Where they should
be placed
◼ What cities they
should pass through
For Example:
◼ Railroad Tracks
◼ Where is a good
central hub
◼ What would cost us
the least and be the
quickest
For Example:
◼ Airports and Flight paths
◼ Where airports should be
◼ What path a flight should take
Example
8 10 14
1 3 5 7
7 4 12 6 3
2
2 4 6 8
9
66
Edge Selection Greedy Strategy
67
Kruskal’s Method
8 10 14
1 3 5 7 1 3 5 7
7 4 12 6 3
2
2 4 6 8 2 4 6 8
9
68
Kruskal’s Method
8 10 14 3
1 3 5 7 1 5 7
7 3 7 4 6 3
2 4 12 6 2
2 4 6 8 2 4 6 8
9
8 10 14 10 14
5 1 3 5 7
1 3 7
7 3 7 4 6 3
2 4 12 6 2
2 4 6 8 2 4 6 8
9
70
Kruskal’s Method
8 10 14 10 14
1 3 5 7 1 3 5 7
7 3 7 4 6 3
2 4 12 6 2
2 4 6 8 2 4 6 8
9
71
Pseudocode For Kruskal’s Method
72
Prim’s Method
8 10 14 10 14
1 3 5 7
1 3 5 7
7 3 7 4 6 3
2 4 12 6 2
4 6 8 2 4 6 8
2
9
73
Time Complexity
◼ Prim’s method.
74