0% found this document useful (0 votes)
12 views143 pages

15 Graphs

Uploaded by

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

15 Graphs

Uploaded by

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

1

2
3
2

Nodes/vertices

4
2

Edges Undirected graph

5
2

Edges Directed graph

6
2 6
5
3
4

1
7

5 2

Edges Undirected graph with


weighted edges

7
8
9
10
11
12
13
14
15
16
17
18
19
20
1 2

5 4
1 2 3 4 5
1 0 1 0 1 0
2 0 0 0 0 1
3 1 0 0 0 0
4 0 0 1 0 0
5 0 0 0 0 0
21
5
1 2
7

3
2 4
3

5 4
1 2 3 4 5
1   
5 4
2   2
3 7   
4  3  
5  
22
23
24
25
26
1 2 4 1 2

2 5 3

3 1
5 4

4 3

27
1 2 4 1 2

2 5 3

3 1
5 4

4 3
An adjacency list
5

Total memory for adjacency lists is


O(|E|)

28
1 2
1 2 4
3
2 5

3 1 5 4

4 3
Array of pointers to
Adjacency lists
5

Total memory for array of pointers is


O(|V|)
29
1 2 4 1 2

2 5 3

3 1
5 4

4 3

Total memory for adjacency list representation is


O(|V|+|E|) which is linear in the size of the
graph. 30
1 2 4 1 2

2 5 3

3 1
5 4

4 3

5
If the edges have weights they
can be stored in the list
elements.

31
32
33
34
35
36
37
CS305

CS402

CS201

CS204 CS300

CS307

38
CS305

CS402

CS201

CS202 CS300

Vertex with
in-degree 0 CS307

39
CS305

CS402

CS201

CS204 CS301

Vertices with
CS307 out-degree 0

40
CS305

CS402

CS201

CS204 CS300

CS307

41
CS305

CS402

CS201

CS204 CS300

CS307

CS201
42
CS305

CS402

CS204 CS300

CS307

CS201
43
CS305

CS402

CS204 CS300

CS307

CS201
44
CS305

CS402

CS204 CS300

CS307

CS201,CS204
45
CS305

CS402

CS300

CS307

CS201,CS204
46
CS305

CS402

CS300

CS307

CS201,CS204
47
CS305

CS402

CS300

CS307

CS201,CS204,CS305
48
CS402

CS300

CS307

CS201,CS202,CS305
49
CS402

CS300

CS307

CS201,CS204,CS305 50
CS402

CS300

CS307

CS201,CS204,CS305,CS307 51
CS402

CS300

CS201,CS204,CS305,CS307
52
CS402

CS300

CS201,CS204,CS305,CS307
53
CS402

CS300

CS201,CS204,CS305,CS307,CS300
54
CS402

CS201,CS204,CS305,CS307,CS300
55
CS402

CS201,CS204,CS305,CS307,CS300
56
CS402

CS201,CS204,CS305,CS307,CS300,CS402
57
CS201,CS204,CS305,CS307,CS300,CS402
58
59
void Graph::topsort( )
{
Vertex v,w;
for (int counter=0; counter < NUM_VERTICES; counter++)
{
// find a vertex with in-degree = 0
v = findNewVertexOfDegreeZero( );

// there is a cycle if no such vertex exists


if (v == NOT_A_VERTEX)
throw CycleFound();

v.topNum = counter;
// the next loop depends on the graph representation
// so it is in pseudocode
for each w adjacent to v
w.indegree --;
}
60
}
61
62
void Graph::topsort( )
{
Queue q (NUM_VERTICES);
int counter = 0;
Vertex v,w;
// pseudocode for initialization
for each vertex v
if (v.indegree == 0) q.enqueue(v);
// Check vertices in the queues
while ( ! q.isEmpty( ) ) {
v = q.dequeue( );
v.topNum = ++counter;
// put new 0 indegree vertices to queue
for each w adjacent to v
if (––w.indegree == 0) q.enqueue( w);
}
// At the end, check for cycles
if (counter != NUM_VERTICES) throw CycleFound( );
} 63
64
N 1

c
i 1
i ,i 1

65
2
v1 v2
4 1 3 10

2 2
v3 v4 v5
8 4
6
5
1
v6 v7

66
2
v1 v2
4 1 3 10

2 2
v3 v4 v5

8 4
6
5
1
v6 v7

Cost = 1+8 = 9
67
2
v1 v2
4 1 3 10

2 2
v3 v4 v5

8 4
6
5
1
v6 v7

Cost = 2+10+6+1 = 19

68
2
v1 v2
4 1 3 10

2 2
v3 v4 v5

8 4
6
5
1
v6 v7

Cost = 2+3+4+1 = 10

69
2
v1 v2
4 1 3 10

2 2
v3 v4 v5

8 4
6
5
1
v6 v7

Cost = 2+3+2+6 +1= 14


70
2
v1 v2
4 1 3 10

2 2
v3 v4 v5

8 4
6
5
1
v6 v7

....... Cost = 1+4+1= 6 (Shortest Path)

71
Edges with negative costs may cause problems!
2
v1 v2
4 1 3 –10

2 1
v3 v4 v5

6 2
6
5
1
v6 v7

72
Edges with negative costs will cause problems!
2
v1 v2
4 1 3 –10

2 1
v3 v4 v5

6 2
6
5
1
v6 v7

73
Edges with negative costs will cause problems!
2
v1 v2
4 1 3 –10

2 1
v3 v4 v5

6 2
6
5
1
v6 v7

Cost = 1+3 –10+1 = –5

74
Edges with negative costs will cause problems!
2
v1 v2
4 1 3 –10

2 1
v3 v4 v5

6 2
6
5
1
v6 v7

Cost = 1+3 –10+1 = –5, in fact we can go around the


Cycle any number of times and cost would be less and less!
75
Edges with negative costs will cause problems!
2
v1 v2
4 1 3 –10

2 1
v3 v4 v5

6 2
6
5
1
v6 v7

Negative cost cycle


Shortest paths are not defined if there is such a cycle.
76
77
78
79
v1 v2

v3 v4 v5

v6 v7

Just like the general problem except all costs are 1!

80
v1 v2

s v3 v4 v5

v6 v7

Suppose we want to shortest paths from s=v3

81
v1 v2

s v3 v4 v5

v6 v7

From v3 to v3 shortest path is 0

82
1
v1 v2

s v3 v4 v5

v6 v7

Let us consider vertices 1 away.


83
1 2
v1 v2

s v3 v4 v5
2
0

v6 v7

Let us consider vertices 2 away by considering yet


unknown vertices that are 1 away from v1 and v6
84
1 2
v1 v2

s v3 v4 v5
2
0

v6 v7

Let us consider vertices 2 away by considering yet


unknown vertices that are 1 away from v1 and v6
1 2
v1 v2

s v3 v4 v5
2
0

v6 v7

Let us consider vertices 3 away by considering yet


unknown vertices that are 1 away from v2 and v4
1 2
v1 v2

s v3 v4 v5
2
0

v6 v7
3
1

Let us consider vertices 3 away by considering yet


unknown vertices that are 1 away from v2 and v4
1
2
v1 v2

s v3 v4 v5
2
0

v6 v7
3
1

Final shortest paths from s


88
1
2
v1 v2

s v3 v4 v5
2
0

v6 v7
3
1

This strategy is known as breadth-first search

The edges make up the breadth-first search tree


89
90
void Graph::unweighted (Vertex s)
{
Vertex v, w;
s.dist = 0;
// check all possible distances
for (int currDist = 0; currDist < NUM_VERTICES; currDist++)
// locate all not known vertices at the current distance
for each vertex v
if ( ! v.known && v.dist == currDist) {
// mark vertex as known
v.known = true;
// Compute distances to neighboring vert.
for each w adjacent to v
if (w.dist == INFINITY) {
w.dist = currDist + 1;
w.path = v;
}
}
} 91
void Graph::unweighted (Vertex s)
{
Vertex v, w;
s.dist = 0;
// check all possible distances
for (int currDist = 0; currDist < NUM_VERTICES; currDist++)
// locate all not known vertices at the current distance
for each vertex v
if ( ! v.known && v.dist == currDist) {
// mark vertex as known
v.known = true;
// Compute distances to neighboring vert.
for each w adjacent to v
if (w.dist == INFINITY) {
w.dist = currDist + 1;
w.path = v;
}
Running time } is O(|V|2)
} 92
93
void Graph::unweighted (Vertex s)
{
Queue q(NUM_VERTICES);
Vertex v, w;
q.enqueue (s); // start the process
s.dist = 0;

while ( ! q.isEmpty( ) ) {
v = q.dequeue( );
for each w adjacent to v
if (w.dist == INFINITY) {
w.dist = v.dist+1;
w.path = v;
q.enqueue (w);
}
}
Note1: Vertices are queued in order of distance from s
}
void Graph::unweighted (Vertex s)
{
Queue q(NUM_VERTICES);
Vertex v, w;
q.enqueue (s); // start the process
s.dist = 0;

while ( ! q.isEmpty( ) ) {
v = q.dequeue( );
for each w adjacent to v
if (w.dist == INFINITY) {
w.dist = v.dist+1;
w.path = v;
q.enqueue (w);
}
} Note1: Vertices are queued in order of distance from s
} Note2: If queue becomes empty prematurely, this indicates there
are nodes that are not reachable from the start node s.
void Graph::unweighted (Vertex s)
{
Queue q(NUM_VERTICES);
Vertex v, w;
q.enqueue (s); // start the process
s.dist = 0;

while ( ! q.isEmpty( ) ) {
v = q.dequeue( );
for each w adjacent to v
if (w.dist == INFINITY) {
w.dist = v.dist+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136
137
138
139
140
141
142
143

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy