Академический Документы
Профессиональный Документы
Культура Документы
■ Cycle, tour.
scheduling tasks precedence constraints
■ Tree, forest. H I L M
software systems functions function calls ■ Connected, connected component.
internet web pages hyperlinks
games board positions legal moves
#include "GRAPH.h" B C G J K
K I
M L
int main(int argc, char *argv[]) {
D E L M
int V = atoi(argv[1]); D
int E = atoi(argv[2]); F F E
Graph G = GRAPHrand(V, E);
GRAPHshow(G);
printf("%d component(s)\n", GRAPHcc(G)); Set of edges representation.
return 0; ■ { A-B, A-G, A-C, L-M, J-M, J-L, J-K, E-D, F-D, H-I, F-E, A-F, G-E }.
}
8 9
Adjacency Matrix Representation Graph ADT Implementation: Adjacency Matrix
GRAPH.h
Adjacency matrix representation.
#include <stdlib.h>
■ Two-dimensional V × V array. #include "GRAPH.h"
■ Edge v-w in graph: adj[v][w] = adj[w][v] = 1. struct graph {
int V; // # vertices
int E; // # edges
A B C D E F G H I J K L M int **adj; // V × V adjacency matrix
A
0 A 0 1 1 0 0 1 1 0 0 0 0 0 0 };
1 B 1 0 0 0 0 1 1 0 0 0 0 0 0
2 C 1 0 0 0 0 0 0 0 0 0 0 0 0 Graph GRAPHinit(int V) {
B C G 3 D 0 0 0 0 1 1 0 0 0 0 0 0 0 Graph G = malloc(sizeof *G);
4 E 0 0 0 1 0 1 1 0 0 0 0 0 0 G->V = V; G->E = 0;
5 F 1 1 0 1 1 0 0 0 0 0 0 0 0 G->adj = MATRIXinit(V, V, 0);
D E 6 G 1 1 0 0 1 0 0 0 0 0 0 0 0 return G;
7 H 0 0 0 0 0 0 0 0 1 0 0 0 0 }
F
8 I 0 0 0 0 0 0 0 1 0 0 0 0 0
J K 9 J 0 0 0 0 0 0 0 0 0 0 1 1 1 void GRAPHinsertE(Graph G, Edge e) {
10 K 0 0 0 0 0 0 0 0 0 1 0 0 0 int v = e.v, w = e.w;
11 L 0 0 0 0 0 0 0 0 0 1 0 0 1 if (G->adj[v][w] == 0) G->E++; no parallel edges
H I L M 12 M 0 0 0 0 0 0 0 0 0 1 0 1 0 G->adj[v][w] = G->adj[w][v] = 1;
Adjacency Matrix }
10 11
14 15
Basis for solving difficult graph problems. // return 1 if s and t in same connected component
■ Biconnectivity. int GRAPHconnect(int s, int t) {
return mark[s] == mark[t];
■ Planarity. }
16 17
Depth First Search: Connected Components Connected Components
Depth First Search: Adjacency Matrix PATHS. Is there a path from s to t?
void dfsR(Graph G, int v, int id) {
int w;
mark[v] = id; Method Preprocess Query Space
for (w = 0; w < G->V; w++)
Union Find O(E log* V) O(log* V) O(V)
if (G->adj[v][w] != 0 && mark[w] == UNMARKED)
dfsR(G, w, id); DFS O(E + V) O(1) O(V)
}
Breadth-first search.
■ Put unvisited nodes on a QUEUE.
Solution. BFS.
■ Initialize mark[s] = 0.
■ When considering edge v-w:
– if w is marked then ignore
DFS.
– if w not marked, set mark[w] = mark[v] + 1
■ Mark ENTRY and EXIT halls at each vertex.
■ Leave by ENTRY when no unmarked halls.
20 21
Breadth First Search Related Graph Search Problems
PATHS. Is there a path from s to t?
Breadth First Search ■ Solution: DFS, BFS, any graph search.
bfs(Graph G, int s) {
link t; SHORTEST PATH. Find shortest path (fewest edges) from s to t.
int v, w; ■ Solution: BFS.
QUEUEput(s);
mark[s] = 0;
while (!QUEUEempty()) { CYCLE. Is there a cycle in the graph?
v = QUEUEget(); ■ Solution: DFS. See textbook.
for (t = G->adj[v]; t != NULL; t = t->next) {
w = t->v;
if (mark[w] == UNMARKED) { EULER TOUR. Is there a cycle that uses each edge exactly once?
mark[w] = mark[v] + 1; ■ Yes if connected and degrees of all vertices are even.
QUEUEput(w); ■ See textbook to find tour.
}
}
} HAMILTON TOUR. Is there a cycle that uses each vertex exactly once?
} ■ Solution: ??? (NP-complete)
22 23