Coder Social home page Coder Social logo

apostolidoum / dynamic_programming_exercise_1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nkyparissas/dynamic_programming_exercise_1

0.0 0.0 0.0 523 KB

This program was implemented as part of the Dynamic Programming undergraduate course at the Technical University of Crete. By using the method of Dynamic Programming, the program finds the shortest path in a weighted graph based on a multi-level decision problem. The program can find the shortest path in any kind of acyclic weighted graph, including the one required by the exercise's rules.

License: GNU General Public License v3.0

C 100.00%

dynamic_programming_exercise_1's Introduction

Dynamic_Programming_Exercise_1

ΕΥΡΕΣΗ ΒΕΛΤΙΣΤΗΣ ΔΙΑΔΡΟΜΗΣ ΣΕ ΓΡΑΦΟ

  1. έστω γράφος της μορφής (παράδειγμα σημειώσεων)

  2. Ερώτηση στο χρήστη ή εισαγωγή από αρχείο: Πόσες βαθμίδες έχει το πρόβλημα;

    numberOfVa8m;	
    B[numberOfVa8m];
    
  3. Ερώτηση στο χρήστη ή εισαγωγή από αρχείο: Πόσοι κόμβοι σε κάθε βαθμίδα;

    numberOfNodes = 0; //συνολικός αριθμός κόμβων
    for( i = 0; i == numberOfVa8m; i++){
    	//fill B[i]
    	//update numberOfNodes
    }	
    

  4. Δημιουργία πίνακα που απεικονίζει το γράφο

    βρίσκουμε μέγιστο στοιχείο του πίνακα Β και δημιουργούμε πίνακα G με διαστάσεις (numberOfVa8m)x(max)

    max = 0;
    for (i = 0; i==numberOfVa8m; i++){
    	if(B[i] > max) 
     	max = B[i]
    }
    

    γεμίζουμε το πίνακα G (κατά στήλες), βάζουμε -1 στις θέσεις όπου δεν υπάρχει κόμβος και ονοματίζουμε τους κόμβους με έναν αύξοντα αριθμό

    node = 0;
    for (i = 0; i == numberOfVa8m; i++){
     	for(j = 0; j < max; j++){
     		if(j<B[i]{
     			G[j,i] = node;
     			node++;	
     		} else G[j,i] = -1;
     	}
    }
    

  5. Εκτύπωση του Γράφου

    printGraph(){
    	for (i = 0; i < max; i++){
    		for (j = 0; j < numberOfVa8m; j++){
    			if(G[i,j] > -1) print G[i,j]
    			else print " "
    		}
    		print \n
    	}
    }
    
  6. Ερώτηση - Εισαγωγή από τον χρήστη : κόστος άμεσης μετάβασης

    for (i = 0; i < numberOfNodes; i++){
    	for(j = 0; j <numberOfNodes; j++){
    		if (i > j ) Cost[i,j] = -1; //κάτω τριγωνικός
    		else if (i = j ) Cost[i,j] = 0; //διαγώνιος
    		else {
    			print is there a path from node i to node j ?
    			fill Cost [i,j]
    		}		
    	}
    }
    

  7. Επίλυση του προβλήματος, δημιουργία πίνακα OPT και NextNode

    κόμβος OPT[i,j] είναι το κόστος διαδρομής από τον κόμβο i στον κόμβο στόχο μέσω του κόμβου j

    στην τελευταία στήλη του πίνακα OPT αποθηκεύεται το βέλτιστο κόστος διαδρομής μέχρι τον τελικό κόμβο, δηλαδή το min της γραμμής

    στον πίνακα NextNode αποθηκεύται η τιμή του j για το οποίο βρέθηκε η min τιμή

    ο πίνακας OPT του δυναμικού προγραμματισμού γεμίζει από κάτω προς τα πάνω γραμμή γραμμή

    	/*
    	* initialize OPT table
    	* copy Cost table
    	*/
    	for (i = 0; i < numberOfNodes; i++){
    		for (j = 0; j < numberOfNodes ; j++){ 
    				 OPT[i,j] = Cost[i,j];
    		}
    	}
    	
    			
    	for( i = numberOfNodes-1; i>=0; i--){
    		min = + 20000000000000000 //arbitrarily large number
    		for( j = 0; j < numberOfNodes; j++){
    			if (COST[i,j]>0){
    			
    				OPT[i,j] = Cost[i,j] + OPT[j,numberOfNodes-1];
    				if (OPT[i,j] <= min {
    					OPT[i,numberOfNodes-1] = OPT[i,j];
    					NextNode[i] = j;					
    				}
    			}
    		}
    	}
    

  8. Εκτύπωση Βέλτιστου Κόστους διαδρομής

    Για το συνολικό πρόβλημα: από κόμβο 0 -> τελικό κόμβο

    τελικός κόμβος-στόχος είναι πάντα ο κόμβος numberOfNodes-1

    BestCost(startNode){
    	print OPT[startNode,nunumberOfNodes-1];
    }
    
  9. Εύρεση μονοπατιού

    από όποιον κόμβο του γράφου

    τελικός κόμβος-στόχος είναι πάντα ο κόμβος numberOfNodes-1

    BestPath(startNode){
    	print startNode
    	visitNextNode = nextNode[startNode];
    	while(visitNextNode != numberOfNodes-1){
    		print visitNextNode;
    		visitNextNode =  nextNode[visitNextNode];
    	}
    }
    

dynamic_programming_exercise_1's People

Contributors

apostolidoum avatar nkyparissas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.