Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Estructura de Datos
C
Los puentes de Königsberg
•
Grafos
Un grafo es un set de vértices y de conexiones (arcos o aristas)
• G = (V,E)
Hermosillo
Nogales
Aplicaciones
• Mapas
• Moléculas
• Horarios
• Redes computacionales
• Donde:
Hermosillo Nogales
Grafos dirigidos
• Directed graphs
3 7
2 4 6
V = { 2, 3, 4, 5, 6, 7}
5 es adyacente a 3
5 3 3 es adyacente desde 5
5 es adyacente a/desde 3
5 3 3 es adyacente desde/a 5
un Path desde 1 a 4:
< 1-2, 2-3, 3-4 >
1 2
2 3
3 4 4
1 2
E = V * (V-1)
3 4
E = V * (V-1) / 2
2 3
4
Hotel
Casa de San
6 la Cultura Sebastián
4
5 8
7 Hospital
Unison Infantil
4
3
4 IMSS #2 6
Grafos
• Implementación:
• Basado en Arreglos
5
8
7 Hospital
Unison Infantil
4
3
4 IMSS #2 6
Grafos
• Implementación en base a Arreglos
VÉRTICES ARCOS (EDGES)
Hotel San 4 4
4
Sebastián
Casa de la 3 5 4
3
Cultura
2 4 4
2 IMSS#2
1 7 6
Hospital
1
Infantil 0 8 3 6
0 Unison 0 1 2 3 4
Grafos
• Implementación basada en Arreglos
• Uso de memoria:
• O(V+V2) = O(V2)
• Ventaja:
• Desventaja:
#define MAXVERTICES 6
int vertices[MAXVERTICES];
int edges[MAXVERTICES][MAXVERTICES];
} GRAPH;
Grafos
• Grafos basados en arreglos
if (g != NULL)
g->edges[from][to] = weight;
}
Grafos
• Grafos basados en arreglos / Revisar si existe un arco entre dos vértices
if (g != NULL)
if(g->edges[from][to] > 0)
return TRUE;
}
Grafos
• Representación basada en Listas ligadas:
• Usar una lista ligada para cada vértice que tenga arcos
(conexiones) hacia otros vértices
4
Hotel
Casa de San
6 la Cultura Sebastián
4
5
8
7 Hospital
Unison Infantil
4
3
4 IMSS #2 6
Grafos
• Implementación en base a Listas Ligadas
VÉRTICES ARCOS (EDGES)
4:Hotel San
3:4
Sebastián
• Uso de memoria:
• O(V+E)
• Ventaja:
• Desventaja:
int vertice;
int weight;
} EDGE;
int vertice;
} VERTICE;
Grafos
typedef struct grafo
int num_vertices;
} GRAPH;
Grafos
void insert(GRAPH *grafo)
A = rand_interval( 1, MAXVERTICES);
B = rand_interval( 1, MAXVERTICES);
insert_edge(grafo, A, B, weight);
}
Grafos
EDGE * existe_edge(GRAPH *g, int from, int to)
VERTICE *p = g->head;
while (p != NULL)
if (p->vertice == from)
return edge;
p = p -> sigv;
return NULL;
}
Grafos
EDGE * get_edge(EDGE *e, int to)
EDGE *p = e;
while ( p != NULL)
if ( p->vertice == to )
return p;
p = p->sige;
return NULL;
}
Búsqueda en grafos
• Depth-First-Search (DFS)
• (Búsqueda en profundidad)
• Breadth-First-Search (BFS)
• (Búsqueda en anchura)
Depth-First-Search (DFS)
• Algoritmo:
2 3
4 5
Depth-First-Search (DFS)
vertice:5 visto
stack:2
1
vertice:2 visto
stack:3
stack:1
stack:4
2 3
vertice:3 visto
stack:1
stack:4
stack:1
stack:4
vertice:1 visto
stack:4
4 5 stack:1
stack:4
vertice: 4 ¡ENCONTRADO!
Depth-First-Search (DFS)
if (encontrado == TRUE)
int encontrado = FALSE;
vertice = pop(&S);
encontrado = TRUE;
} else {
mark_it(g, vertice);
push_adjacent_edges(&S, g, vertice);
display() display_edges()
push()
pop()
dfs() mark_it()
push_adjacent
get_vertice()
_vertices()
Breadth - First Search
• http://en.wikipedia.org/wiki/Breadth-first_search
encontrado = TRUE;
} else {
mark_it(g, vertice);
push_adjacent_edges(&S, g, vertice);
display() display_edges()
add()
remove()
dfs()
mark_it()
add_adjacent
_vertices() get_vertice()
• Algunas diapositivas tomadas de:
• https://sites.google.com/site/atulkg/courses/es-103-data-
structure-and-algorithms-2012
• http://www.cse.unr.edu/~bebis/CS302/
• https://www.cs.princeton.edu/~rywang/99f126/