lecture12 graphs
TRANSCRIPT
-
7/25/2019 Lecture12 Graphs
1/51
1
Lecture 12: Graphs
-
7/25/2019 Lecture12 Graphs
2/51
2
Graph Terminology
vertex, node, point
edge, line, arc
G = (V, E)
V is set of vertices
E is set of edges
Each edge joins two different vertices
2
1
3
5
4
-
7/25/2019 Lecture12 Graphs
3/51
3
Undirected Graph edges do not have a direction
The edge from 1 to 2 is also an edge
from 2 to 1 Edge (1, 2) implies that there is also
an edge (2, 1) [ The same edge ]
2
1
3
5
4
-
7/25/2019 Lecture12 Graphs
4/51
4
Directed Graph edges have a direction
Edge (2, 1) means only that there is an
edge from 2 to 1 In this example,there is no edge from 1
to 2
2
1
3
5
4
-
7/25/2019 Lecture12 Graphs
5/51
5
Weighted Graph
weights (values) are associated with the
edges in the graph
may be directed for undirected
Weighted graphs are also referred to as
networks
2
1
3
5
4
12 13
15 16
3222
-
7/25/2019 Lecture12 Graphs
6/51
6
Complete Graph
For each pair of vertices, there is one
edge
If G = (V, E) is a complete graph, and
|V| = n, then can you calculate |E|?
2
1
3
4
-
7/25/2019 Lecture12 Graphs
7/51
7
Subgraph
A subgraph G of graph G = (V, E) is a
graph (V, E) that V V and E E.
2
1
3
4
-
7/25/2019 Lecture12 Graphs
8/51
8
Path
the sequence of edges (i1, i2), (i2,
i3), ,(ik-1, ik).
Denoted as path i1, i2, ..., ik
Simple pathall vertices (expect
possibly first and last) are different
Length of path is sum of the lengths of
the edges
-
7/25/2019 Lecture12 Graphs
9/51
9
Examples of graph
-
7/25/2019 Lecture12 Graphs
10/51
10
Representation of Graphs
Adjacency matrix
Incidence matrix
Adjacency lists: Table, Linked List
Space/time trade-offs depending on
which operation are most frequent as
well as properties of the graph
-
7/25/2019 Lecture12 Graphs
11/51
11
-
7/25/2019 Lecture12 Graphs
12/51
12
Can we use tree traversal
algorithms to traverse graphs?
Why?
Loops in graphs, parent/childrelation in trees
-
7/25/2019 Lecture12 Graphs
13/51
13
Depth first search
DFS(v)
num(v)=i++;
for all vertices u adjacent to v
if num(u) is 0
attach edge(uv) to edges;DFS(u);
depthFirstSearch()
for all vertices v
num(v)=0;edges=null; i=1;
while there is a vertex v such that num(v) is 0
DFS(v);
output edges
-
7/25/2019 Lecture12 Graphs
14/51
14
-
7/25/2019 Lecture12 Graphs
15/51
15
-
7/25/2019 Lecture12 Graphs
16/51
16
Breadth first searchbreadthFirstSearch()
for all vertices u
num(u)=0;
Edges=null; i=1;
While there is a vertex v such that num(v)==0
num(V)=i++;
enqueue(v);
while queue is not empty
v=dequeue();
for all vertices u adjacent to vif num(u) is 0
num(u)=i++;
enqueue(u);
attach edge(uv) to edges;
Output edges;
-
7/25/2019 Lecture12 Graphs
17/51
17
-
7/25/2019 Lecture12 Graphs
18/51
18
-
7/25/2019 Lecture12 Graphs
19/51
19
An Example
4
2
5 6
1 3
What would the visit orders for
DFS(1), DFS(5), BFS(1), BFS(5)
look like?
-
7/25/2019 Lecture12 Graphs
20/51
20
Unweighted Shortest Path
Find the shortest path (measured by
number of edges) from a designated
vertex S to every vertex Simplified case of weighted shortest
path
-
7/25/2019 Lecture12 Graphs
21/51
21
Algorithm
Starting from node S
Distance from S to S is 0, so label as 0
Find all nodes which are distance 1 from S Label as distance 1
Find all nodes which are distance 2 from S
These are 1 step from those labeled 1 This is precisely a breadth first search
-
7/25/2019 Lecture12 Graphs
22/51
22
An Example
2
7
51
3
6
4
What are the values of distance[2], distance[3], ,
distance[7] after FindShortestPath(G, 1) is executed?
-
7/25/2019 Lecture12 Graphs
23/51
23
Positive Weighted Shortest Path
Length is sum of the edges costs on thepath
All edges have nonnegative cost Find shortest paths from some start
vertex to all vertices
similar process to unweighted case Dijkstra's Algorithm(Single source
shortest path)
-
7/25/2019 Lecture12 Graphs
24/51
24
Distance at each node v is shortest path
distance from s to v using only knownvertices as intermediates
An example of a Greedy Algorithm
Solve problem in stages by doing whatappears to be the best thing at eachstage
Decisionin one stage is not changed
laterA simple greedy example is counting
change (quarters, then dimes,)
-
7/25/2019 Lecture12 Graphs
25/51
25
Greedy algorithms do not always work
Change breaks down with addition
of .12 coin ( try .15 value )
They do work for certain problems and
are fairly simple
-
7/25/2019 Lecture12 Graphs
26/51
26
DijkstraAlgorithm(weighted Simpledigraph, vertexfirst)
for all verticesv
currDist(v)=infinity;currDist(first)=0;
toBeChecked=all vertices;
WhiletoBeChecked is not empty
v = a vertex intoBeChecked with minimalcurrDist(v);
removev fromtoBeChecked;
for all verticesu adjacentto v and intoBeChecked
if currDist(u)>currDist(v)+weight(edge(uv))
currDist(u)=currDist(v)+weight(edge(uv));
predecessor(u)=v;
-
7/25/2019 Lecture12 Graphs
27/51
27
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
start at v1, all distances are infinity
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
mark v1 as known, with distance 0
0
-
7/25/2019 Lecture12 Graphs
28/51
28
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0
Now adjust distances for v2 and v4
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
1
-
7/25/2019 Lecture12 Graphs
29/51
29
Select v4 as known, since it has the lowest cost among unknown
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
1
Examine adjacent, adjust v3, v5, v6 and v7
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
1
3 3
9 5
-
7/25/2019 Lecture12 Graphs
30/51
30
Select v2 as known
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
1
3 3
9 5
Check v4, v5. v4 is final, v5 has lower cost of 3 vs. 12
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
1
3 3
9 5
-
7/25/2019 Lecture12 Graphs
31/51
31
Select v5 as known
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
1
3 3
9 5Check v7, but not adjusted
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
1
3 3
9 5
-
7/25/2019 Lecture12 Graphs
32/51
32
v3 is selected as known
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
1
3 3
9 5
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
1
3 3
8 5
Check v1 and v6. v1 known, v6 adjusted from 9 to 8
-
7/25/2019 Lecture12 Graphs
33/51
33
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
13 3
8 5
Select v7 as known
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
1
3 3
6 5
Check v6, change from 8 to 6
-
7/25/2019 Lecture12 Graphs
34/51
34
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
1
3 3
6 5
Select v6 as known
2
7
53
1
6
4
10
6
22
2
4
15
1 3
8 4
0 2
1
3 3
6 5
Nothing to check
Done
-
7/25/2019 Lecture12 Graphs
35/51
35
Spanning tree
subgraph of G
contains all vertices of G
connected graph with no cycles
-
7/25/2019 Lecture12 Graphs
36/51
36
Examples of spanning trees
-
7/25/2019 Lecture12 Graphs
37/51
37
Minimum spanning tree
spanning tree with minimum cost
only exists if G is connected
number of edges is |V|-1 three greedy methods
Kruskal's algorithm
Dijkstras method Prim's algorithm
differ in how next edge is selected
-
7/25/2019 Lecture12 Graphs
38/51
38
Kruskal's algorithm
KruskalAlgorithm( weighted connectedundirectedgraph)
tree = null;edges = sequence of all edges ofgraphsorted by weight;
for(i=1;i
-
7/25/2019 Lecture12 Graphs
39/51
39
-
7/25/2019 Lecture12 Graphs
40/51
40
2
7
53
1
6
4
10
6
72
24
15
1 3
8 4
Construct MST for this graph using Kruskal's algorithm starting
v1
2
7
53
1
6
4
10
6
72
2
4
15
13
8 4
-
7/25/2019 Lecture12 Graphs
41/51
41
2
7
53
1
6
4
10
6
72
2
4
15
1 3
8 4
Construct MST for this graph using Kruskal's algorithm starting
v1
2
7
53
1
6
4
10
6
72
2
4
15
13
8 4
-
7/25/2019 Lecture12 Graphs
42/51
42
2
7
53
1
6
4
10
6
72
2
4
15
1 3
8 4
Construct MST for this graph using Kruskal's algorithm starting
v1
2
7
53
1
6
4
10
6
72
2
4
15
13
8 4
-
7/25/2019 Lecture12 Graphs
43/51
43
Construct MST for this graph using Kruskal's algorithm starting
v1
2
7
53
1
6
4
10
6
72
2
4
15
13
8 4
-
7/25/2019 Lecture12 Graphs
44/51
44
Dijkstras method
DijkstraMethod( weighted connected undirectedgraph)
tree = null;
edges = an unsorted sequence of all edges ofgraph;
for j=1 to |E|
addei to tree;
if there is a cycle in treeremove an edge with maximum
weight from this only cycle;
-
7/25/2019 Lecture12 Graphs
45/51
45
-
7/25/2019 Lecture12 Graphs
46/51
46
Prim's algorithm
Similar to the Dijkstras algorithm inshortest paths
grow the tree in successive stages in each stage, one node is picked as the
root, we add an edge, and thus a vertexis added to the tree
have a set on vertices in the tree and aset that is not in the tree
-
7/25/2019 Lecture12 Graphs
47/51
47
Prim's algorithm (cont.)
at each stage, a new vertex to add tothe tree is selected by choosing edge (u,
v) such that the cost of (u,v) is thesmallest among all edges where u is inthe tree and v is not
Build spanning tree starting from v1
Result in the same spanning tree asthat given by the Kruskal algorithm
-
7/25/2019 Lecture12 Graphs
48/51
48
2
7
53
1
6
4
10
6
72
2
4
15
1 3
8 4
Construct MST from v1 for this graph using Prim's algorithm
2
7
53
1
6
4
10
6
72
2
4
15
1 3
8 4
-
7/25/2019 Lecture12 Graphs
49/51
49
2
7
53
1
6
4
10
6
72
2
4
15
1 3
8 4
Construct MST from v1 for this graph using Prim's algorithm
2
7
53
1
6
4
10
6
72
2
4
15
1 3
8 4
-
7/25/2019 Lecture12 Graphs
50/51
50
2
7
53
1
6
4
10
6
72
2
4
15
1 3
8 4
Construct MST from v1 for this graph using Prim's algorithm
2
7
53
1
6
4
10
6
72
2
4
15
1 3
8 4
-
7/25/2019 Lecture12 Graphs
51/51
51
Construct MST from v1 for this graph using Prim's algorithm
2
7
53
1
6
4
10
6
72
2
4
15
1 3
8 4
Same MST as Kruskal