De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

Implementatie Zoekboom

Verwante presentaties


Presentatie over: "Implementatie Zoekboom"— Transcript van de presentatie:

1 Implementatie Zoekboom
implementeert de interface class Boom : IColl { private string midden; 14 private Boom links, rechts; public bool Zoek(string s) { if (this==null) return false; Recursieve aanroep: de method roept zichzelf aan Recursieve aanroep: de method roept zichzelf aan if (s == this.midden) return true; if (s < this.midden) return links.Zoek(s); else return rechts.Zoek(s); } }

2 Analyse Benodigde tijd bij n elementen Ongeordende rij zoek n stappen
voegtoe 1 stap Gesorteerde rij zoek 2log(n) stappen voegtoe n stappen De logaritmische zoektijd in een gesorteerde rij danken we aan het feit dat het zoekgebied in elke stap halveert. Bij zoeken in een zoekboom is dat ook zo: bij elke stap valt de helft af, dus de zoektijd is de logaritme van het aantal elementen in de boom. Omdat een element toevoegen aan een zoekboom op dezelfde manier verloopt als zoeken, kan ook het toevoegen in logaritmische tijd gebeuren. Dat is veel beter dan in een gesorteerde rij, waar het opschuiven van elementen veel tijd kost. Weliswaar kan het toevoegen in een ongeordende rij nog net iets sneller, maar daar duurt het zoeken juist weer erg lang. De zoekboom combineert dus de voordelen van de twee andere methoden. Zoekboom zoek 2log(n) stappen voegtoe 2log(n) stappen

3 Zoekboom wortel diepte 4 23 15 29 10 3 1 6 11 5 8 18 26 34 14
class Boom : IColl { private string midden; private Boom links, rechts; public int Diepte( ) { if (this==null) return 0; int d1 = links.Diepte(); int d2 = rechts.Diepte(); return 1+Math.Max(d1, d2); } public int Totaal( ) { if (this==null) return 0; int t1 = links.Totaal(); int t2 = rechts.Totaal(); return t1 + t2 + this.midden; } }

4 Propositie- Formule } /\ /\ \/ not /\ not /\ x \/ z y y z x y
interface IFormule { Propositie- Formule string ToString(); void Verzamel(ISet<string>); bool Waarde(Valuatie v); } class ... : IFormule {...} /\ /\ \/ not /\ not /\ x \/ z y y z x y

5 SatSolver class Program {
static void Main( ) { while (true) { try { String invoer = Console.ReadLine(); Iformule formule = Parser . ParseFormule (invoer); Valuatie valuatie = Solver . Vervulbaar (formule); if (valuatie==null) Console.WriteLine( "UNSAT" ); else Console.WriteLine( "SAT\n" + valuatie . ToString () ); } catch (Exception e) { Console.WriteLine( "FOUT " + e.Message ); } break; } // Console.ReadLine(); }

6 class Valuatie { SortedDictionary<string,bool> dict;
void VoegToe(string variabele, bool waarde) { } bool GeefWaarde(string variabele) bool BevatWaarde(string variabele) string ToString( ) dict . Add (variabele, waarde); return dict[variabele]; dict . ContainsKey(variabele); string res = ""; foreach( KeyValuePair<string,bool> paar in dict ) res += paar.Key + "=" + paar.Value ; return res;

7 Propositie- Formule } /\ /\ \/ not /\ not /\ x \/ z y y z x y
interface IFormule { Propositie- Formule string ToString(); void Verzamel(ISet<string>); bool Waarde(Valuatie v); } class ... : IFormule {...} /\ /\ \/ not /\ not /\ x \/ z y y z x y

8 class Solver { static Valuatie Vervulbaar (IFormule formule) {
ISet<string> alleVars; alleVars= new HashSet<string>( ); formule . Verzamel ( alleVars ); Valuatie nogNiets = new Valuatie( ); return Solver.Vervul ( formule, , .... ); alleVars, nogNiets ); } static Valuatie Vervul (IFormule formule , ISet<string> variabelen, Valuatie valuatie) { if (variabelen Count==0) return .....; else Roep de functie recursief aan met een kleinere set variabelen en een iets grotere valuatie tweemaal maar alleen als dat kansrijk is }

9 Propositie- Formule } /\ /\ \/ not /\ not /\ x \/ z y y z x y
interface IFormule { Propositie- Formule string ToString(); void Verzamel(ISet<string>); bool MogelijkWaar(Valuatie v); } class ... : IFormule {...} /\ /\ \/ not /\ not /\ x \/ z y y z x y


Download ppt "Implementatie Zoekboom"

Verwante presentaties


Ads door Google