Sunteți pe pagina 1din 51

Lecture 12: Graphs

Graph Terminology
2

1
3

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

1
2

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 ]
3

1
2

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
4

12
2

22

15

1
3

13
32

16

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
5

1
2

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|?

1
2

Subgraph
A subgraph G of graph G = (V, E) is a
graph (V, E) that V V and E E.

Path
the sequence of edges (i1, i2), (i2,
i3), ,(ik-1, ik).
Denoted as path i1, i2, ..., ik
Simple path all vertices (expect
possibly first and last) are different
Length of path is sum of the lengths of
the edges

Examples of graph

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

10

11

Can we use tree traversal


algorithms to traverse graphs?
Why?
Loops in graphs, parent/child
relation in trees

12

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

13

14

15

Breadth first search


breadthFirstSearch()
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 v
if num(u) is 0
num(u)=i++;
enqueue(u);
attach edge(uv) to edges;
Output edges;

16

17

18

An Example
1

What would the visit orders for


DFS(1), DFS(5), BFS(1), BFS(5)
look like?
19

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

20

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
21

An Example

3
1

2
4

5
7

What are the values of distance[2], distance[3], ,


distance[7] after FindShortestPath(G, 1) is executed?
22

Positive Weighted Shortest Path


Length is sum of the edges costs on the
path
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)

23

Distance at each node v is shortest path


distance from s to v using only known
vertices as intermediates
An example of a Greedy Algorithm
Solve problem in stages by doing what
appears to be the best thing at each
stage
Decision in one stage is not changed
later
A simple greedy example is counting
change (quarters, then dimes,)

24

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

25

DijkstraAlgorithm(weighted Simple digraph, vertex


first)
for all vertices v
currDist(v)=infinity;
currDist(first)=0;
toBeChecked=all vertices;
While toBeChecked is not empty
v = a vertex in toBeChecked with minimal
currDist(v);
remove v from toBeChecked;
for all vertices u adjacent to v and in
toBeChecked
if currDist(u)>currDist(v)+weight(edge(uv))
currDist(u)=currDist(v)+weight(edge(uv));
predecessor(u)=v;

26

start at v1, all distances are infinity

8
5

2
6

mark v1 as known, with distance 0


0
2
1
2
4
3
1
2
2
3
4

8
5

4
1

10

10
5

4
1

6
27

0
1

2
3

2
4

8
5

10

4
1

Now adjust distances for v2 and v4


0
1

2
2

2
1
4

8
5

10

4
1

6
28

Select v4 as known, since it has the lowest cost among unknown


0
1

2
2

2
1
4

8
5

10

4
1

Examine adjacent, adjust v3, v5, v6 and v7


0
2
2
1
2
10
4
3
1 1
2
2
4
5 3
3 3
8
4
5
6
1
6
7
9
5

29

Select v2 as known
0
1

3 3

2
2

1
4

10

5 3

6
1
6
7
9
5
Check v4, v5. v4 is final, v5 has lower cost of 3 vs. 12
0
2
2
1
2
10
4
3
1 1
2
2
4
5 3
3 3
8
4
5
6
1
6
7
9
5

30

Select v5 as known

0
1

3 3

2
2

2
1
4

1
6
9
Check v7, but not adjusted
0
1

2
2

1
4

8
5

6
9

5 3
6

7
5

3 3

10

10

5 3

4
1

7
5

6
31

v3 is selected as known

0
1

3 3

2
2

1
4

10
5 3

6
1
6
7
9
5
Check v1 and v6. v1 known, v6 adjusted from 9 to 8
0
2
2
1
2
10
4
1
3
1
2
2
4
5 3
3 3
8
4
5
6
1
6
7
8
5

32

Select v7 as known
0
1
4
1
2
3 3

2
2

2
1
4

8
5

6
8

10

5 3

4
1

Check v6, change from 8 to 6


0
2
1
4
1
3
1
2
4
3 3
8
4
5
1
6
6

7
5
2
2

10

2
7
5

5 3
6
33

Select v6 as known
0
1
4
1
2
3 3

2
2

2
1
4

8
1

Done

6
6

5 3

2
2

2
1

7
5

1
4

8
5

6
6
Nothing to check
0
1
4
2
3 3

10

10

5 3

4
1

7
5

6
34

Spanning tree
subgraph of G
contains all vertices of G
connected graph with no cycles

35

Examples of spanning trees

36

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


37

Kruskal's algorithm
KruskalAlgorithm( weighted connected
undirected graph)
tree = null;
edges = sequence of all edges of graph
sorted by weight;
for(i=1;i<= |E| and |tree| < |V|-1; i++)
if e i from edges does not form a cycle with
edges in tree
add e i to tree;
38

39

Construct MST for this graph using Kruskal's algorithm starting


v1
2
1
2
10
4
3
1
2
7
3
4
5
8
4
5
6
1
6
7

8
5

2
3

10

4
1

6
40

Construct MST for this graph using Kruskal's algorithm starting


v1
2
1
2
10
4
3
1
2
7
3
4
5
8
4
5
6
1
6
7

8
5

2
3

10

4
1

6
41

Construct MST for this graph using Kruskal's algorithm starting


v1
2
1
2
10
4
3
1
2
7
3
4
5
8
4
5
6
1
6
7

8
5

2
3

10

4
1

6
42

Construct MST for this graph using Kruskal's algorithm starting


v1

8
5

2
3

10

4
1

6
43

Dijkstras method
DijkstraMethod( weighted connected undirected
graph)
tree = null;
edges = an unsorted sequence of all edges of
graph;
for j=1 to |E|
add e i to tree;
if there is a cycle in tree
remove an edge with maximum
weight from this only cycle;
44

45

Prim's algorithm
Similar to the Dijkstras algorithm in
shortest paths
grow the tree in successive stages
in each stage, one node is picked as the
root, we add an edge, and thus a vertex
is added to the tree
have a set on vertices in the tree and a
set that is not in the tree

46

Prim's algorithm (cont.)


at each stage, a new vertex to add to
the tree is selected by choosing edge (u,
v) such that the cost of (u,v) is the
smallest among all edges where u is in
the tree and v is not
Build spanning tree starting from v1
Result in the same spanning tree as
that given by the Kruskal algorithm

47

Construct MST from v1 for this graph using Prim's algorithm


2

8
5

4
8

4
1

10

10

4
1

6
48

Construct MST from v1 for this graph using Prim's algorithm


2

8
5

4
8

4
1

10

10

4
1

6
49

Construct MST from v1 for this graph using Prim's algorithm


2

8
5

4
8

4
1

10

10

4
1

6
50

Construct MST from v1 for this graph using Prim's algorithm

Same MST as Kruskal

4
8

10

4
1

6
51

S-ar putea să vă placă și