adjList.c

26 Novembre 2008
#include <stdio.h>
#include <stdlib.h>

struct vertex {
                int value;
                struct vertex *next;
                struct edge *list;
};
typedef struct vertex *Vertex;

struct edge {
                Vertex to; 
                struct edge *next;
};
typedef struct edge *Edge;

struct graph {
                int V; 
                int E;
                struct vertex *head;
};
typedef struct graph *Graph;

Graph GRAPHinit() {
	Graph G = malloc(sizeof *G);
	G->V=0;
	G->E=0;
	G->head=NULL;
	return G;
}

void GRAPHinsertV(Graph G, int value) {
	Vertex newV = malloc(sizeof newV);
	newV->value = value;
	newV->next = G->head;
	newV->list = NULL;
	G->head = newV;
	G->V++;
}

void GRAPHinsertEfromPointers(Graph G, Vertex V, Vertex W)
{
	Edge tmp = malloc(sizeof(struct edge));
	tmp->to = W;
	tmp->next = V->list;
	V->list = tmp;
}

Vertex findVertexFromValue(Graph G, int value)
{
	Vertex tmp;
	for(tmp = G->head;tmp!=NULL;tmp = tmp->next)
	{
		if(tmp->value==value)
			return tmp;
	}
	return NULL;
}

void GRAPHinsertEfromValues(Graph G, int valueV, int valueW)
{
	Vertex V, W;
	V = findVertexFromValue(G, valueV);
	W = findVertexFromValue(G, valueW);
	if(V!=NULL && W!=NULL)
	{
		GRAPHinsertEfromPointers(G, V, W);
	} else {
		fprintf(stderr, "\nValori non trovati\n");
	}
}

void GRAPHprint(Graph G)
{
	Vertex tmp;
	Edge list;
	for(tmp = G->head;tmp!=NULL;tmp=tmp->next)
	{
		fprintf(stdout,"V:%d\t",tmp->value);
		for(list = tmp->list;list!=NULL;list=list->next)
			fprintf(stdout,"%d\t",list->to->value);
		fprintf(stdout, "\n");
	}
}

int main()
{
	Graph G = GRAPHinit();
	GRAPHinsertV(G, 1);
	GRAPHinsertV(G, 3);
	GRAPHinsertV(G, 13);
	GRAPHinsertV(G, 6);
	GRAPHinsertV(G, 21);
	GRAPHinsertV(G, 41);
	GRAPHinsertEfromValues(G, 41,21);
	GRAPHinsertEfromValues(G, 41,13);
	GRAPHinsertEfromValues(G, 41,6);
	GRAPHinsertEfromValues(G, 1,21);
	GRAPHinsertEfromValues(G, 1,13);
	GRAPHprint(G);
	free(G);
}

syntax highlighted by Code2HTML, v. 0.9.1

main.c and data file for adjMatrix

17 Novembre 2008
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include "GRAPH.h"

void replace(char *in, char *what, char by_what)
{
	int what_len = strlen(what);
	for(;;)
	{
		char *str = strstr(in, what);
		if (!str)
			break;
		memcpy(str + 1, str + what_len, strlen(str + what_len) + 1);
		*str = by_what;
	}
}

void usage(void)
{
	fprintf(stdout, "-f filename   \tcreate graph from file filename\n");
	fprintf(stdout, "-s begin,end  \tsearch a path in the graph from begin to end\n");
	fprintf(stdout, "-o            \toriented graph\n");
	fprintf(stdout, "-w            \tweighted graph\n");
	fprintf(stdout, "-h            \tthis help\n");
}

int main(int argc, char *argv[])
{
	char *filename = NULL;
	char *tmp = NULL, *a, *b;
	int sw;
	int begin=-1;
	int end=-1;
	int oriented=0;
	int weighted=0;
	while((sw = getopt(argc, argv, "f:s:owh")) != -1)
	{
		switch(sw)
		{
			case 'f':
				filename = optarg;
				break;

		        case 's':
			        tmp = strtok(optarg, ", ");
				begin = atoi(tmp);
				tmp = strtok(NULL, ", ");
				end = atoi(tmp);
				break;

		        case 'o':
			        oriented=1;
				break;

		        case 'w':
			        weighted=1;
				break;

			case 'h':
			default:
				usage();
				return 1;
		}
	}

	Graph G;

	if(filename!=NULL)
	  {
	    fprintf(stdout, "Loading graph from file: %s\n", filename);
	    G = GRAPHloadfromfile(filename, weighted, oriented);
	  }

	if(begin!=-1)
	  {
	    fprintf(stdout, "Finding path between node %d and %d: \n", begin, end);
	    GRAPHpathsearch(G, begin, end);
	  }
	return 0;
}

30
1 2 1
0 3 5
5 7 8
12 29 10
11 13 1
23 22 3
21 18 76
21 19 3
19 17 4
17 6 3


syntax highlighted by Code2HTML, v. 0.9.1

adjMatrix.c

17 Novembre 2008
#include <stdio.h>
#include <stdlib.h>
#include "GRAPH.h"

#define MAX_LINE 20
/*#define DEBUG 1*/

struct graph {int V; int E; int **adj;};
Edge EDGE(int v, int w, int weight)
{
  Edge E;
  E.v = v;
  E.w = w;
  E.weight = weight;
  return E;
}

Graph GRAPHinit(int V)
{
  Graph G = malloc(sizeof *G) ;
  G->V = V; G->E = 0;
  G->adj = MATRIXint(V, V, 0);
  return G;
}

void GRAPHinsertE(Graph G, Edge e, int oriented)
{
  int v = e.v, w = e.w, weight = e.weight;
  if (G->adj[v][w] == 0) G->E++;
  G->adj[v][w] = weight;
  if(!oriented)
    G->adj[w][v] = weight;
}

void GRAPHremoveE(Graph G, Edge e, int oriented)
{
  int v = e.v, w = e.w, weight = e.weight;
  if (G->adj[v] [w] == 1) G->E--;
  G->adj[v][w] = 0;
  if(!oriented)
    G->adj[w][v] = 0;
}

int GRAPHedges(Edge a[], Graph G)
{
  int v, w, E = 0;
  for (v = 0; v < G->V; v++)
    for (w = v+1; w < G->V; w++)
      if (G->adj[v][w] >= 1)
        a[E++] = EDGE(v, w, G->adj[v][w]);
  return E;
}

int **MATRIXint(int r, int c, int val)
{
  int i, j;
  int **t = malloc(r * sizeof(int *));
  for (i = 0; i < r; i++)
    t[i] = malloc(c * sizeof(int));
  for (i = 0; i < r; i++)
    for (j = 0; j < c; j++)
      t[i][j] = val;
  return t;
}

void GRAPHprint(Graph G)
{
  int v, w, E = 0;
  for (v = 0; v < G->V; v++)
    {
      fprintf(stdout, "%d\n", v);
      for (w = v+1; w < G->V; w++)
      {
	if (G->adj[v][w] >= 1)
	  fprintf(stdout, "\t=>%d: %d\t", w, G->adj[v][w]);
      }
    }
}

Graph GRAPHloadfromfile(char *filename, int weighted, int oriented)
{
  FILE *fp;
  char line[MAX_LINE];
  Graph G;
  int nodes, v, w;
  //weight value for arc
  int wVal = 1;
  if((fp=fopen(filename, "r"))==NULL)
    {
      fprintf(stdout, "Unable to open %s\nEXITING!\n", filename);
      exit(0);
    }
  fgets(line, MAX_LINE, fp);
  sscanf(line, "%d", &nodes);
  G = GRAPHinit(nodes);

  while(!feof(fp))
    {
      fgets(line, MAX_LINE, fp);
      if(weighted)
	sscanf(line,"%d %d %d", &v, &w, &wVal);
      else
	sscanf(line, "%d %d", &v, &w);
      if(oriented)
	GRAPHinsertE(G, EDGE(v,w,wVal), 1);
      else
	GRAPHinsertE(G, EDGE(v,w,wVal), 0);
    }
  fclose(fp);
  return G;
}

/*
  search for a path
  value 0: starting node
  value -1: unvisited node
*/
int GRAPHpathsearch(Graph G, int begin, int end)
{
  int i, v, w;
  int nodes[G->V];
  for(i=0; i<G->V; i++)
    nodes[i] = -1;
  GRAPHpathrecursivelabel(G, nodes, begin, end, 0);

  #ifdef DEBUG
    fprintf(stdout, "Nodes: %d\n", G->V);
    for(i=0; i<G->V; i++)
      fprintf(stdout, "%d: %d\n", i, nodes[i]);
  #endif

  if(nodes[end]!=-1)
    fprintf(stdout, "PATH EXISTS!\n");
  else
    fprintf(stdout, "PATH DOES NOT EXISTS!\n");
}

int GRAPHpathrecursivelabel(Graph G, int *nodes, int from, int to, int value)
{
  int w, i;

  #ifdef DEBUG
    fprintf(stdout, "Nodes: %d\n", G->V);
    for(i=0; i<G->V; i++)
      fprintf(stdout, "%d: %d\n", i, nodes[i]);
  #endif

  if(nodes[from]>=0)
    return to;
  nodes[from]=value;
  if(from==to)
      return to;
  for(w=0; w<G->V; w++)
    {

      #ifdef DEBUG
	fprintf(stdout, "Trying: %d=>%d = %d\n", from, w, G->adj[from][w]);
	getchar();
      #endif

      if(G->adj[from][w]!=0 && nodes[to]<0)
	{
	  GRAPHpathrecursivelabel(G, nodes, w, to, value+1);
	}
    }
  return from;
}

syntax highlighted by Code2HTML, v. 0.9.1

GRAPH.h

17 Novembre 2008
typedef struct { int v; int w; int weight;} Edge;
Edge EDGE(int, int, int);
typedef struct graph *Graph;
Graph GRAPHinit(int);
void  GRAPHinsertE(Graph, Edge, int);
void  GRAPHremoveE(Graph, Edge, int);
int   GRAPHedges(Edge [], Graph G);
Graph GRAPHcopy(Graph);
void  GRAPHdestroy(Graph);
void  GRAPHprint(Graph G);
Graph GRAPHloadfromfile(char *filename, int weighted, int oriented);

int **MATRIXint(int, int, int);

//graph path
int GRAPHpathsearch(Graph G, int begin, int end);
int GRAPHpathrecursivelabel(Graph G, int *vert, int from, int to, int value);

syntax highlighted by Code2HTML, v. 0.9.1

keyStructInt.h

17 Novembre 2008
#ifndef __STDIOH__
#include <stdio.h>
#define __STDIOH__ 1
#endif

#ifndef __STDLIBH__
#include <stdlib.h>
#define __STDLIBH__ 1
#endif

typedef struct keyStruct {
  int intKey;
}keyStruct;
#define __KEYSTRUCT__ 1

keyStruct* createKeyStruct(int x);
void copyVal(keyStruct *from, keyStruct *to);
char* printVal(keyStruct *val);

syntax highlighted by Code2HTML, v. 0.9.1

keyStructInt.c

17 Novembre 2008
#include "keyStructInt.h"

keyStruct* createKeyStruct(int x)
{
  keyStruct *newKey = (keyStruct *)malloc(sizeof(keyStruct));
  newKey->intKey = x;
  return newKey;
}

void copyVal(keyStruct *from, keyStruct *to)
{
  to->intKey = from->intKey;
}
char* printVal(keyStruct *val)
{
  char* str = malloc(20*sizeof(char));
  sprintf(str, "%d", (int)val->intKey);
  return str;
}

syntax highlighted by Code2HTML, v. 0.9.1

keyStructString.h

17 Novembre 2008
#ifndef __STDIOH__
#include <stdio.h>
#define __STDIOH__ 1
#endif

#ifndef __STDLIBH__
#include <stdlib.h>
#define __STDLIBH__ 1
#endif

#ifndef _STRINGH__
#include <string.h>
#endif

typedef struct keyStruct {
  char *stringKey;
}keyStruct;

#define __KEYSTRUCT__ 1

keyStruct* createKeyStruct(char *x);
void copyVal(keyStruct *from, keyStruct *to);
char* printVal(keyStruct *val);

syntax highlighted by Code2HTML, v. 0.9.1

keyStructString.c

17 Novembre 2008
#include "keyStructString.h"

keyStruct* createKeyStruct(char *x)
{
  keyStruct *newKey = (keyStruct *)malloc(sizeof(keyStruct));
  newKey->stringKey = malloc(sizeof(x));
  strcpy(newKey->stringKey, x);
  return newKey;
}

void copyVal(keyStruct *from, keyStruct *to)
{
  to->stringKey = malloc(sizeof(from->stringKey));
  strcpy(to->stringKey, from->stringKey);
}

char* printVal(keyStruct *val)
{
  return val->stringKey;
}

syntax highlighted by Code2HTML, v. 0.9.1

queuestruct.c

17 Novembre 2008
#include "queueStruct.h"

#define DEBUG 1

element* enqueue(
	      element *pTail,
	      keyStruct *val
	     )
{
      element *pNew = (element*)malloc(sizeof(element));
      pNew->key = val;
      if(pTail==NULL)
	{
	  pTail=pNew;
	  pTail->next = pTail;
#ifdef DEBUG
	  fprintf(stdout, "QUEUE OPENED\n");
#endif
	}

      else
	{
	  pNew->next=pTail->next;
	  pTail->next=pNew;
	  pTail = pNew;
#ifdef DEBUG
	  fprintf(stdout, "VALUE ADDED\n");
#endif
	}
      return pTail;
}

element* dequeue(
	      element *pTail,
	      keyStruct *val,
	      int *status
	      )
{
  element *pOld;
  char *str;
  if(pTail!=NULL)
    {
      *status=SUCCESS;
      if(pTail->next==pTail)
	{
	  KS_copyValues(pTail->key, val);
#ifdef DEBUG
	  fprintf(stdout, "%s\tQUEUE EMPTY\n", printVal(val));
#endif
	  free(pTail);
	  pTail=NULL;
	}
      else
	{
	  pOld = pTail->next;
	  pTail->next = pOld->next;
	  KS_copyValues(pOld->key, val);
#ifdef DEBUG
	  fprintf(stdout, "%s\n", KS_formatOut(val, str));
#endif
	  free(pOld);
	}
    }
  else
    {
      *status = FAILURE;
    }
  return pTail;
}

syntax highlighted by Code2HTML, v. 0.9.1

queuestruct.h

17 Novembre 2008
#ifndef __STDIOH__
#include <stdio.h>
#define __STDIOH__ 1
#endif

#ifndef __STDLIBH__
#include <stdlib.h>
#define __STDLIBH__ 1
#endif

#ifndef __KEYSTRUCT__
#include "keyStructString.h"
#endif

#define FAILURE 0
#define SUCCESS 1

typedef struct element{
  keyStruct *key;
  struct element *next;
}element;

element* enqueue(element *pTail, keyStruct *val);
element* dequeue(element *pTail, keyStruct *val, int *status);

syntax highlighted by Code2HTML, v. 0.9.1