From eff9b7e3ac6f894e03d644bfba564909a260794c Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Fri, 17 Mar 2017 21:00:23 +0100 Subject: Grafi --- DFS.cpp | 39 +++++++++++++++++++++++ Es 3.cpp | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Es5.cpp | 46 +++++++++++++++++++++++++++ Es6.cpp | 38 ++++++++++++++++++++++ es4.cpp | 67 +++++++++++++++++++++++++++++++++++++++ main.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 372 insertions(+) create mode 100644 DFS.cpp create mode 100644 Es 3.cpp create mode 100644 Es5.cpp create mode 100644 Es6.cpp create mode 100644 es4.cpp create mode 100644 main.cpp diff --git a/DFS.cpp b/DFS.cpp new file mode 100644 index 0000000..fda5a33 --- /dev/null +++ b/DFS.cpp @@ -0,0 +1,39 @@ +#include +#define BIANCO 0 //Non Visitato +#define NERO 2 //Visitato +#define GRIGIO 1 //Non Finito +#define INF 999999 +#include +#include +#include +#define MAXN 9999 +#define INDEF -1 +#include +using namespace std; +int V=9; +int matriceAdj[MAXN][MAXN]; + int colore[MAXN]; + //DFS Tramite matrice di adiacenza + void dfsVisit (int u) + { + colore[u]= GRIGIO; + for (int i=0;i +#define BIANCO 0 //Non Visitato +#define NERO 2 //Visitato +#define GRIGIO 1 //Non Finito +#define INF 999999 +#include +#include +#include +#define MAXN 9999 +#define INDEF -1 +#include +using namespace std; +typedef pair p; +std::priority_queue, std::greater

> Q; +int V,E; +int matriceAdj[MAXN][MAXN]; +int T; +struct nodo { + vector adj; + int inizio,fine; +}no[MAXN]; + int colore[MAXN]; + bool Ciclico=false; + bool dfsVisit (int u) + { + cout<>V>>E; + int a,b; + for (int i=0;i>a>>b; + no[a].adj.push_back(b); + } + dfs(); + for (int i=0;i +#define BIANCO 0 //Non Visitato +#define NERO 2 //Visitato +#define GRIGIO 1 //Non Finito +#define INF 999999 +#include +#include +#include +#define MAXN 9999 +#define INDEF -1 +#include +using namespace std; +typedef pair p; +struct nodo{ + vector adj; + vector p; +}; + +int main(int argc, char** argv) { + int v,e; + int sorgente; + nodo no[v]; + + priority_queue

Q; + int peso[v]; + Q.push(p(0,sorgente)); + + while(!Q.empty()) + { + pair a=Q.top(); + Q.pop(); + if (a.first > peso[a.second]) + continue; + + for (int i=0;ia.first+ no[a.second].p[i]) + { + peso[no[a.second].adj[i]]=a.first + no[a.second].p[i]; + Q.push(p(peso[no[a.second].adj[i]],no[a.second].adj[i])); + } + } + + } + return 0; +} diff --git a/Es6.cpp b/Es6.cpp new file mode 100644 index 0000000..9907eb9 --- /dev/null +++ b/Es6.cpp @@ -0,0 +1,38 @@ +#include +#include +using namespace std; +/* run this program using the console pauser or add your own getch, system("pause") or input loop */ + +int main(int argc, char** argv) { + int v=5; + int matrice[v][v]; + for (int i=0;i +#define BIANCO 0 //Non Visitato +#define NERO 2 //Visitato +#define GRIGIO 1 //Non Finito +#define INF 999999 +#include +#include +#include +#define MAXN 9999 +#define INDEF -1 +#include +using namespace std; +typedef pair p; +int V,E; + +struct nodo { + vector

adj; + int dist; + nodo(){ + dist=INF; + } +}no[MAXN]; + int colore[MAXN]; + void relax (nodo no[], int u, int z) + { + p v= no[u].adj[z]; + if (no[v.first].dist> no[u].dist + v.second ) + { + no[v.first].dist= no[u].dist + v.second; + } + } + + void bf (nodo no[],int n, int s) + { + int i,j,z; + no[0].dist=0; + for (i=0;i>V>>E; + int a,b,c; + for (int i=0;i>a>>b>>c; + no[a].adj.push_back(p(b,c)); + } + bf(no,V,0); //Adiacenze, numeri di vertici, sorgente + for (int i=0;i +#define BIANCO 0 //Non Visitato +#define NERO 1 //Visitato +#define GRIGIO //Non Finito +#define INF 999999 +#include +#include +#include +#define MAXN 9999 +#define INDEF -1 +#include +using namespace std; +int V=9; //Nodi +int precedente[MAXN]; //Vettore su cui viene salvato il percorso per arrivare alla destinazione +struct nodo { + vector adj; +}no[MAXN];; + + int bfsVisit (int s) + { + int dist[V]; + int colore[V]; + queue q; + + for (int i=0;i sta; + int i=3; // arrivo + sta.push(i); + while (precedente[i]!=INDEF) + { + + } + */ //Procedura iterativa per stampare il percorso fino al nodo sorgente + + } + + void stampaPercorso (int j) //Procedura ricorsiva j=precedente[destinazione] + { + if (precedente[j] != INDEF ) + stampaPercorso(precedente[j]); + cout<