Source code online books for professionals by professionals



Download 4,67 Mb.
Pdf ko'rish
bet61/266
Sana31.12.2021
Hajmi4,67 Mb.
#213682
1   ...   57   58   59   60   61   62   63   64   ...   266
Bog'liq
2 5296731884800181221

Listing 4-9.  A Naïve Algorithm for Topological Sorting
def naive_topsort(G, S=None):
    if S is None: S = set(G)                    # Default: All nodes
    if len(S) == 1: return list(S)              # Base case, single node
    v = S.pop()                                 # Reduction: Remove a node
    seq = naive_topsort(G, S)                   # Recursion (assumption), n-1
    min_i = 0
    for i, u in enumerate(seq):
        if v in G[u]: min_i = i+1               # After all dependencies
    seq.insert(min_i, v)
    return seq
 
Although I hope it’s clear (by induction) that naive_topsort is correct, it is also clearly quadratic (by recurrence 
2 from Table 3-1). The problem is that it chooses an arbitrary node at each step, which means that it has to look 
where the node fits after the recursive call (which gives the linear work). We can turn this around and work more like 
selection sort. Find the right node to remove before the recursive call. This new idea, however, leaves us with two 
questions. First, which node should we remove? And second, how can we find it efficiently?
13
We’re working with a sequence (or at least we’re working toward a sequence), which should perhaps give us an 
idea. We can do something similar to what we do in selection sort and pick out the element that should be placed first 
(or last ... it doesn’t really matter; see Exercise 4-19). Here, we can’t just place it first—we need to really remove it from 
the graph, so the rest is still a DAG (an equivalent but smaller problem). Luckily, we can do this without changing the 
graph representation directly, as you’ll see in a minute.
How would you find a node that can be put first? There could be more than one valid choice, but it doesn’t matter 
which one you take. I hope this reminds you of the maximum permutation problem. Once again, we want to find the 
nodes that have no in-edges. A node without in-edges can safely be placed first because it doesn’t depend on any 
others. If we (conceptually) remove all its out-edges, the remaining graph, with n–1 nodes, will also be a DAG that can 
be sorted in the same way.
Tip
 

  If a problem reminds you of a problem or an algorithm you already know, that’s probably a good sign. In fact, 
building a mental archive of problems and algorithms is one of the things that can make you a skilled algorist. If you’re 
faced with a problem and you have no immediate associations, you could systematically consider any relevant  
(or semirelevant) techniques you know and look for reduction potential.
Just like in the maximum permutation problem, we can find the nodes without in-edges by counting. By 
maintaining our counts from one step to the next, we need not start fresh each time, which reduces the linear step 
cost to a constant one (yielding a linear running time in total, as in recurrence 1 in Table 3-1). Listing 4-10 shows an 
iterative implementation of this counting-based topological sorting. (Can you see how the iterative structure still 
embodies the recursive idea?) The only assumption about the graph representation is that we can iterate over the 
nodes and their neighbors.
13
Without effective selection, we’re not gaining anything. For example, the algorithms I’ve compared with, insertion and selection 
sort, are both quadratic, because selecting the largest or smallest element among unsorted elements isn’t any easier than inserting  
it among sorted ones.


Chapter 4 

 InduCtIon and reCursIon ... and reduCtIon 
84

Download 4,67 Mb.

Do'stlaringiz bilan baham:
1   ...   57   58   59   60   61   62   63   64   ...   266




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