The Algorithm Design Manual Second Edition



Download 5,51 Mb.
Pdf ko'rish
bet142/488
Sana31.12.2021
Hajmi5,51 Mb.
#273936
1   ...   138   139   140   141   142   143   144   145   ...   488
Bog'liq
2008 Book TheAlgorithmDesignManual

5.6.2

Finding Paths

The parent array set within bfs() is very useful for finding interesting paths

through a graph. The vertex that discovered vertex is defined as parent[i].

Every vertex is discovered during the course of traversal, so except for the root

every node has a parent. The parent relation defines a tree of discovery with the

initial search node as the root of the tree.

Because vertices are discovered in order of increasing distance from the root,

this tree has a very important property. The unique tree path from the root to

each node x

∈ V uses the smallest number of edges (or equivalently, intermediate

nodes) possible on any root-to-path in the graph.

We can reconstruct this path by following the chain of ancestors from to the

root. Note that we have to work backward. We cannot find the path from the root

to x, since that does not follow the direction of the parent pointers. Instead, we

must find the path from to the root. Since this is the reverse of how we normally

want the path, we can either (1) store it and then explicitly reverse it using a stack,

or (2) let recursion reverse it for us, as follows:

find_path(int start, int end, int parents[])

{

if ((start == end) || (end == -1))



printf("\n%d",start);

else {


find_path(start,parents[end],parents);

printf(" %d",end);

}

}

On our breadth-first search graph example (Figure



5.9

) our algorithm generated

the following parent relation:

vertex


1

2

3



4

5

6



parent

-1

1



2

5

1



1


166

5 .


G R A P H T R A V E R S A L

For the shortest path from 1 to 4, upper-right corner, this parent relation yields

the path

{154}.

There are two points to remember when using breadth-first search to find a

shortest path from to y: First, the shortest path tree is only useful if BFS was

performed with as the root of the search. Second, BFS gives the shortest path

only if the graph is unweighted. We will present algorithms for finding shortest

paths in weighted graphs in Section

6.3.1

(page


206

).


Download 5,51 Mb.

Do'stlaringiz bilan baham:
1   ...   138   139   140   141   142   143   144   145   ...   488




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish