0% found this document useful (0 votes)
10 views56 pages

Lecture 09

Uploaded by

leezhipheng1049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views56 pages

Lecture 09

Uploaded by

leezhipheng1049
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

1

¢ Graph
¢ Adjacency Matrix

¢ Adjacency List

¢ Greedy algorithms

¢ Making changes with fewest coins

¢ Shortest Distance

¢ Dijkstra’s Algorithm

¢ Minimum Spanning tree

¢ Kruskal’s Algorithm

¢ Prim’s Algorithm

CSC1203 - Data Structures & Algorithms


Graphs

3
¢ A graph is a set of nodes (vertices) joined by
edges (arcs).
¢ A graph may be directed or undirected

¢ A graph is connected if you can get from any


node to any other node by following edges.

CSCI203 - Algorithms and Data Structures


¢A directed graph is strongly connected if
you can get from any node to any other
node by following directed edges.

CSCI203 - Algorithms and Data Structures


¢ Anacyclic graph is one in which there is
no path from any node back to the same
node that does not involve retracing edges

CSCI203 - Algorithms and Data Structures


¢ In a graph, two graph vertices are
adjacent if they are joined by a graph
edge.
¢ The adjacency matrix of a simple graph is
a matrix with rows and columns labeled
by graph vertices, with a 1 or 0 in position
according to whether and are adjacent or
not.

CSCI203 - Algorithms and Data Structures


http://mathworld.wolfram.com/AdjacencyMatrix.html
8

CSCI203 - Algorithms and Data Structures


¢ The adjacency list representation of a graph
consists of n lists one for each vertex.
1 2 3 4
1 1 1 1 1
2 1 0 0 0
3 0 1 0 1
4 0 1 1 0

1 -> 1 -> 2 -> 3 -> 4


2 -> 1
3 -> 2 -> 4
4 -> 2 -> 3

CSCI203 - Algorithms and Data Structures


10

CSC2103 - Data Structures & Algorithms


CSC1203 - Data Structures & Algorithms
11
¢ Optimization problem
— An optimization problem is one in which we
want to find not just a solution but the best
solution.
— “Greedy algorithm” sometimes works well for
optimization problems
— “Greedy algorithm” works in phases. At each
phase
¢ Take the best which can get it now, without regarding future
consequences

12

CSC1203 - Data Structures & Algorithms


¢ Greedy algorithm solves optimization problems
by making locally optimal choices and hoping
that these choices lead to a globally optimal
solution . These algorithms work by taking
what seems to be the best decision at each step.
¢ No backtracking is done (once a choice is made
we are stuck with it)
¢ Easy to design
¢ Easy to implement
¢ Efficient (when they work)

13

CSC1203 - Data Structures & Algorithms


¢ Example 1: making change
¢ Given we have $2, $1, 50¢, 20¢, 10¢, 5¢ and 1¢ coins; what is
the best (fewest coins) way to pay any given amount?
¢ The greedy approach is to pay as much as possible using the
largest coin value possible, repeatedly until the amount is
paid.

¢ E.g. to pay $17.97 we pay 8 $2 coins, 1 $1, 1 50¢ coin, 2 20¢


coins, 1 5¢ coin and 2 1¢ coins (15 coins total).
¢ This is the optimal solution (although this is harder to prove
than you might think).
¢ Note that this algorithm will not work with an arbitrary set
of coin values.
¢ Adding a 12¢ coin would result in 15¢ being made from 1 12¢
and 3 1¢ (4 coins) instead of 1 10¢ and 1 5¢ coin (2 coins).

14

CSC1203 - Data Structures & Algorithms


How Greedy Algorithms Work?
¢ We start with a set of candidates which have not yet
been considered for the solution
¢ As we proceed, we construct two further sets:
— Candidates that have been considered and selected
— Candidates that have been considered and rejected
¢ At each step we check to see if we have reached a
solution
¢ At each step we also check to see if a solution can be
reached at all
¢ At each step we select the best acceptable candidate
from the unconsidered set and move it into the
selected set
¢ We also move any unacceptable candidates into the
rejected set
15

CSC1203 - Data Structures & Algorithms


16
Example 2: Shortest Path
¢ Let G= (N, E) be a connected, directed graph
consisting of a set of nodes N, and a set of
directed edges E.
¢ Each edge has a length, the distance from the
node at one end of the edge to the node at the
other end.
¢ One node is designated the source node
¢ The problem is to find the shortest path from
the source node to each of the other nodes
¢ The most popular shortest path algorithm is
Dijkstra’s Algorithm

17

CSC1203 - Data Structures & Algorithms


For example, find the shortest path from Node 1 to
all other nodes.
1
10 50

5 2
30
100

10
5
20

4 3
50

18

CSC1203 - Data Structures & Algorithms


¢ Dijkstra’s Algorithm: An Example

Step 0 S={1} C = {2, 3, 4, 5}

D = [50, 30, 100, 10]

1
10 50

5 2
30
100

10
5
20

4 3 19
50

CSC1203 - Data Structures & Algorithms


¢ Dijkstra’s Algorithm: An Example

1
10 50

5 2
30
100

10
5
20

4 3 20
50

CSC1203 - Data Structures & Algorithms


¢ Dijkstra’s Algorithm: An Example

Step 0 S = { 1 ,5} C = {2, 3, 4}

D = [50, 30, 100, 10]

1
10 50

5 2
30
D=10 100

10
5
20

End of Step 0
4 3 21
50

CSC1203 - Data Structures & Algorithms


¢ Dijkstra’s Algorithm: An Example

Step 1 S = { 1 ,5} C = {2, 3, 4}

D = [50, 30, 20, 10]

1
10 50

5 2
30
D=10 100

10
5
20

4 3 22
50

CSC1203 - Data Structures & Algorithms


¢ Dijkstra’s Algorithm: An Example

Step 1 S = { 1 ,5} C = {2, 3, 4}

D = [50, 30, 20, 10]

1
10 50

5 2
30
D=10 100

10
5
20

End of Step 1
4 3 23
50
D=20
CSC1203 - Data Structures & Algorithms
¢ Dijkstra’s Algorithm: An Example

Step 2 S = { 1 ,5,4} C = {2, 3}

D = [50, 30, 20, 10]

1
10 50

5 2
30
D=10 100

10
5
20

4 3 24
50
D=20
CSC1203 - Data Structures & Algorithms
¢ Dijkstra’s Algorithm: An Example

Step 2 S = { 1 ,5,4,3} C = {2}

D = [50, 30, 20, 10]

1
10 50

5 2
30
D=10 100

10
5
20
End of Step 2
4 3 25
50 D=30
D=20
CSC1203 - Data Structures & Algorithms
¢ Dijkstra’s Algorithm: An Example

Step 3 S = { 1 ,5,4,3} C = {2}

D = [50, 30, 20, 10]

1
10 50

5 2
30
D=10 100

10
5
20

4 3 26
50 D=30
D=20
CSC1203 - Data Structures & Algorithms
¢ Dijkstra’s Algorithm: An Example

Step 3 S = { 1 ,5,4,3} C = {2}

D = [40, 30, 20, 10]

1
10 50

5 2
30
D=10 100

10
5
20

4 3 27
50 D=30
D=20
CSC1203 - Data Structures & Algorithms
¢ Dijkstra’s Algorithm: An Example

Step 3 S = { 1 ,5,4,3} C = {2}

D = [35, 30, 20, 10]

1
10 50

5 2
30
D=10 100

10
5
20

4 3 28
50 D=30
D=20
CSC1203 - Data Structures & Algorithms
¢ Dijkstra’s Algorithm: An Example

Step 3 S = { 1 ,5,4,3,2} C={}

D = [35, 30, 20, 10]

1
10 50

5 2
30
D=10 100 D=35
10
5
20
End of Step 2

4 3 29
50 D=30
D=20
CSC1203 - Data Structures & Algorithms
Dijkstra’s Algorithm
¢ Uses two sets of nodes S and C
¢ At each iteration S contains the set of
nodes that have already been chosen
¢ At each iteration C contains the set of
nodes that have not yet been chosen
¢ At each step we move the node which is
cheapest to reach from C to S
¢ An arrayD contains the shortest path so
far from the source to each node

30

CSC1203 - Data Structures & Algorithms


Initialize array D[2..n]
C→{2,3,..,n}
S →{1} Start node
For i → 2 to n do D[i] → L[1,i] // put initial values into array D
Repeat n-2 times
v → an element of C minimizing D[v]
C → C-{v} // remove v from C
S → S U {v} add v to S
for each w in C do
D[w] → min(D[w],D[v]+L[v,w]) Return D

31

CSC1203 - Data Structures & Algorithms


CSC1203 - Data Structures & Algorithms
32
¢A sub graph that spans (reaches out)
to all vertices of a graph is called
a spanning sub graph.
¢ A sub graph that is a tree and that
spans (reaches out to ) all vertices of the
original graph is called a spanning tree.
¢ °Among all the spanning trees of a
weighted and connected graph, the one
(possibly more) with the least total weight
is called a minimum spanning tree (MST).
33

CSC1203 - Data Structures & Algorithms


¢ Minimum Spanning Tree
— Let G = (N, E) be a connected, undirected graph
consisting of a set of nodes, N, and a set of edges E.
— Each edge has a length, the distance from the node
at one end of the edge to the node at the other end.
— The problem is to find a subset, S, of the edges of G
such that the graph G¢ = (N, S) is still connected
and that the total length of the edges in S is
minimized.
— G¢ is called the minimum spanning tree for the
graph G

34

CSC1203 - Data Structures & Algorithms


¢ Minimum Spanning Tree

1 1 2 2 3 1 1 2 2 3

6 5
4 4 6 4
3
3 8

4 5 6 4 5 6

7
4 3 4 3

G 7 G¢ 7

35

CSC1203 - Data Structures & Algorithms


Minimum Spanning Tree
¢ Two possible paths of attack seem possible:
— Start with an empty set S and select at each
stage the shortest edge that has been neither
selected nor rejected.
— Start at a given node and at each stage select
into S the shortest edge that extends the
graph to a new node
— Strangely, both approaches work

36

CSC1203 - Data Structures & Algorithms


CSC1203 - Data Structures & Algorithms
37
¢ Kruskal’s Algorithm
— Start with an initially empty set of edges S.
— Add edges to S
— At each step add the shortest edge to S which
increases the connectedness of the graph.
— Reject a candidate edge if it does not effect the
connectedness of S.
— Stop when the graph is connected.

38

CSC1203 - Data Structures & Algorithms


¢ Kruskal’s Algorithm: An Example
¢ Step 0 - {1} {2} {3} {4} {5} {6} {7}

1 1 2 2 3 1 2 3

6
4 4 5 6
3

4 5 8 6 4 5 6
7
4
3

G 7 G¢ 7

39

CSC1203 - Data Structures & Algorithms


¢ Kruskal’s Algorithm: An Example
¢ Step 1 {1, 2} {1, 2} {3} {4} {5} {6} {7}

1 2 2 3 1 1 2 3

6 5
4 4 6

3 8

4 5 6 4 5 6

4 7 3

G 7 G¢ 7

40

CSC1203 - Data Structures & Algorithms


¢ Kruskal’s Algorithm: An Example
¢ Step 2 {2, 3} {1, 2, 3} {4} {5} {6} {7}

1 2 2 3 1 1 2 3

6 5
4 4 6
3 8

4 5 6 4 5 6

4 7
3

G 7 G¢ 7

41

CSC1203 - Data Structures & Algorithms


¢ Kruskal’s Algorithm: An Example
¢ Step 3 {4, 5} {1, 2, 3} {4, 5} {6} {7}

1 2 2 3 1 1 2 3

6 5
4 4 6
3 8

4 5 6 4 5 6

4 7 3

G 7 G¢ 7

42

CSC1203 - Data Structures & Algorithms


¢ Kruskal’s Algorithm: An Example
¢ Step 4 {6, 7} {1, 2, 3} {4, 5} {6, 7}

1 2 2 3 1 1 2 3

6 5
4 4 6
3 8

4 5 6 4 5 6

4 7 3

G 7 G¢ 7

43

CSC1203 - Data Structures & Algorithms


¢ Kruskal’s Algorithm: An Example
¢ Step 5 {1, 4} {1, 2, 3, 4, 5} {6, 7}

1 2 2 3 1 1 2 3

6 5
4 4 6
3 8

4 5 6 4 5 6

4 7 3

G 7 G¢ 7

44

CSC1203 - Data Structures & Algorithms


¢ Kruskal’s Algorithm: An Example
¢ Step 5 {2, 5} {1, 2, 3, 4, 5} {6, 7} - rejected

1 2 2 3 1 1 2 3

6 5
4 4 6
3 8

4 5 6 4 5 6

4 7 3

G 7 G¢ 7

45

CSC1203 - Data Structures & Algorithms


¢ Kruskal’s Algorithm: An Example
¢ Step 5 {4, 7} {1, 2, 3, 4, 5, 6, 7} - done

1 2 2 3 1 1 2 3

6 5
4 4 6
3 8

4 5 6 4 5 6

4 7 3

G 7 G¢ 7

46

CSC1203 - Data Structures & Algorithms


1 (Sort the edges in an increasing order)
2 A:={ }
3 while E is not empty do {
3 take an edge (u, v) that is shortest in E
and delete it from E
4 if u and v are in different components
then
add (u, v) to A
}
Note: each time a shortest edge in E is
considered.
47

CSC1203 - Data Structures & Algorithms


CSC1203 - Data Structures & Algorithms
48
Let O be a set of nodes and S a set of edges
Initially O contains the first node of N and S is
empty
At each step look for the shortest edge {u, v} in E
such that u Î O and v Ï O
Add {u, v} to S
Add v to O
Repeat until O = N
Note that, at each step, S is a minimum
spanning tree on the nodes in O

49

CSC1203 - Data Structures & Algorithms


¢ Prim’s Algorithm: An Example
¢ Step 0 - {1}

1 1 2 2 3 1

6
4 4 5 6
3

4 5 8 6
7
4
3

G 7 G¢
50

CSC1203 - Data Structures & Algorithms


¢ Prim’s Algorithm: An Example
¢ Step 1 {1, 2} {1, 2}

1 1 2 2 3 1 2

6
4 4 5 6
3

4 5 8 6
7
4
3

G 7 G¢
51

CSC1203 - Data Structures & Algorithms


¢ Prim’s Algorithm: An Example
¢ Step 2 {2, 3} {1, 2, 3}

1 2 2 3 1 1 2 3

6 5
4 4 6

3 8

4 5 6

4 7 3

G 7 G¢
52

CSC1203 - Data Structures & Algorithms


¢ Prim’s Algorithm: An Example
¢ Step 3 {1, 4} {1, 2, 3, 4}

1 2 3 1 2 3
1 2

6 4 5 6 4

3 8
4 5 6 4

4 7 3

G 7 G¢
53

CSC1203 - Data Structures & Algorithms


¢ Prim’s Algorithm: An Example
¢ Step 4 {4, 5} {1, 2, 3, 4, 5}

1 2 3 1 2 3
1 2

6 4 5 6 4

8 3
4 5 6 4 5

4 7 3

G 7 G¢
54

CSC1203 - Data Structures & Algorithms


¢ Prim’s Algorithm: An Example
¢ Step 5 {4, 7} {1, 2, 3, 4, 5, 7}

1 2 3 1 2 3
1 2

6 4 5 6 4

8 3
4 5 6 4 5

7 3 4

G 7 G¢ 7
55

CSC1203 - Data Structures & Algorithms


¢ Prim’s Algorithm: An Example
¢ Step 5 {7, 6} {1, 2, 3, 4, 5, 6, 7} – done

1 2 3 1 2 3
1 2

6 4 5 6 4

8 3
4 5 6 4 5 6

7 4 3

G 7 G¢ 7
56

CSC1203 - Data Structures & Algorithms

You might also like