Qhov txawv ntawm Kruskal thiab Prim

Qhov txawv ntawm Kruskal thiab Prim
Qhov txawv ntawm Kruskal thiab Prim

Video: Qhov txawv ntawm Kruskal thiab Prim

Video: Qhov txawv ntawm Kruskal thiab Prim
Video: Maiv Xis Tu Siab Thiab Quaj Heev Vim Tus Hluas Nraug Tsi Leeg Txais Ob Niam Tub Lawm 25/12/2020 2024, Kaum ib hlis
Anonim

Kruskal vs Prim

Hauv kev tshawb fawb hauv computer, Prim's thiab Kruskal's algorithms yog qhov kev ntshaw algorithm uas pom qhov tsawg kawg ntawm tsob ntoo rau qhov sib txuas hnyav uas tsis muaj kev taw qhia. Ib tsob ntoo spanning yog ib daim duab ntawm ib daim duab uas txhua qhov ntawm daim duab txuas nrog txoj kev, uas yog tsob ntoo. Txhua tsob ntoo spanning muaj qhov hnyav, thiab qhov tsawg kawg nkaus qhov hnyav / tus nqi ntawm tag nrho cov ntoo spanning yog qhov tsawg kawg nkaus spanning ntoo (MST).

Ntau txog Prim's Algorithm

Lub algorithm tau tsim los ntawm Czech lej Vojtěch Jarník hauv xyoo 1930 thiab tom qab ntawd nws tus kheej los ntawm tus kws tshawb fawb computer Robert C. Prim hauv xyoo 1957. Nws tau rov tshawb pom los ntawm Edsger Dijkstra hauv xyoo 1959. Cov algorithm tuaj yeem hais nyob rau hauv peb kauj ruam tseem ceeb;

Muab cov duab txuas nrog n nodes thiab qhov hnyav ntawm txhua ntug, 1. Xaiv ib qho kev txiav txim siab ntawm daim duab thiab ntxiv rau tsob ntoo T (uas yuav yog thawj ntawm)

2. Xav txog qhov hnyav ntawm txhua ntug txuas nrog cov nodes hauv tsob ntoo thiab xaiv qhov tsawg kawg nkaus. Ntxiv cov ntug thiab cov node ntawm lwm qhov kawg ntawm tsob ntoo T thiab tshem tawm ntug ntawm daim duab. (Xaiv ib qho yog tias muaj ob lossis ntau qhov tsawg kawg nkaus)

3. Rov ua kauj ruam 2, kom txog thaum n-1 npoo ntxiv rau tsob ntoo.

Nyob hauv txoj kev no, tsob ntoo pib nrog ib qho kev txiav txim siab thiab nthuav tawm los ntawm cov node mus rau txhua lub voj voog. Yog li ntawd, rau lub algorithm ua hauj lwm kom zoo, cov duab yuav tsum tau ib tug txuas graph. Daim ntawv tseem ceeb ntawm Prim's algorithm muaj lub sijhawm nyuaj ntawm O(V2).

Ntau txog Kruskal's Algorithm

Lub algorithm tsim los ntawm Joseph Kruskal tau tshwm sim hauv kev sib hais plaub ntawm American Mathematical Society hauv xyoo 1956. Kruskal's algorithm kuj tuaj yeem hais tawm hauv peb kauj ruam yooj yim.

Muab daim duab nrog n nodes thiab qhov hnyav ntawm txhua ntug, 1. Xaiv lub arc nrog qhov hnyav tshaj plaws ntawm tag nrho cov duab thiab ntxiv rau tsob ntoo thiab tshem tawm ntawm daim duab.

2. Ntawm qhov seem xaiv qhov tsawg kawg nkaus qhov hnyav, nyob rau hauv ib txoj kev uas tsis tsim ib lub voj voog. Ntxiv cov ntug rau tsob ntoo thiab tshem tawm ntawm daim duab. (Xaiv ib qho yog tias muaj ob lossis ntau qhov tsawg kawg nkaus)

3. Rov ua cov txheej txheem hauv kauj ruam 2.

Nyob rau hauv txoj kev no, algorithm pib nrog qhov hnyav tsawg kawg nkaus thiab txuas ntxiv xaiv txhua ntug ntawm txhua lub voj voog. Yog li ntawd, nyob rau hauv lub algorithm lub graph yuav tsum tsis txhob txuas. Kruskal's algorithm muaj lub sijhawm nyuaj ntawm O(logV)

Dab tsi yog qhov txawv ntawm Kruskal's thiab Prim's Algorithm?

• Prim's algorithm pib nrog lub node, whereas Kruskal's algorithm pib nrog ntug.

• Prim's algorithms span from one node to another while Kruskal's algorithm select the edges in a way that the position of the edge is not based on the last step.

• Hauv prim's algorithm, daim duab yuav tsum yog daim duab txuas nrog thaum Kruskal's tuaj yeem ua haujlwm ntawm cov duab tsis sib txuas ib yam nkaus.

• Prim's algorithm muaj lub sijhawm nyuaj ntawm O(V2), thiab Kruskal lub sij hawm nyuaj yog O(logV).

Pom zoo: