Presentation is loading. Please wait.

Presentation is loading. Please wait.

96703046 98207429 99703002 [UVa]291 - The House Of Santa Claus.

Similar presentations


Presentation on theme: "96703046 98207429 99703002 [UVa]291 - The House Of Santa Claus."— Presentation transcript:

1 96703046 98207429 99703002 [UVa]291 - The House Of Santa Claus

2 In your childhood you most likely had to solve the riddle of the house of Santa Claus. Do you remember that the importance was on drawing the house in a stretch without lifting the pencil and not drawing a line twice? As a reminder it has to look like shown in Figure 1.

3 [UVa]291 - The House Of Santa Claus Well, a couple of years later, like now, you have to ``draw'' the house again but on the computer. As one possibility is not enough, we require all the possibilities when starting in the lower left corner. Follow the example in Figure 2 while defining your stretch. Figure: This Sequence would give the Output line 153125432

4 [UVa]291 - The House Of Santa Claus All the possibilities have to be listed in the output file by increasing order, meaning that 1234... is listed before 1235.... Output So, an output file could look like this: 12435123 13245123... 15123421

5 (Euler Path)( ) (Euler Cycle) (Euler Chain) Cycle 0 Chain 2

6 Depth-First Search (depth first) : DFS on a graph with n vertices and m edges takes O(n m ) time Depth-first search is to graphs what Euler tour is to binary trees

7 Depth-First Search7 DFS Algorithm The algorithm uses a mechanism for setting and getting labels of vertices and edges Algorithm DFS(G, v) Input graph G and a start vertex v of G Output labeling of the edges of G in the connected component of v as discovery edges and back edges setLabel(v, VISITED) for all e G.incidentEdges(v) if getLabel(e) UNEXPLORED w opposite(v,e) if getLabel(w) UNEXPLORED setLabel(e, DISCOVERY) DFS(G, w) else setLabel(e, BACK) Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges for all u G.vertices() setLabel(u, UNEXPLORED) for all e G.edges() setLabel(e, UNEXPLORED) for all v G.vertices() if getLabel(v) UNEXPLORED DFS(G, v)

8 Example Depth-First Search8 DB A C E D B A C ED B A C E discovery edge back edge A visited vertex A unexplored vertex unexplored edge

9 Example (cont.) Depth-First Search9 DB A C E DB A C E DB A C E D B A C E

10 10 Properties of DFS Property 1 DFS(G, v) visits all the vertices and edges in the connected component of v Property 2 The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v DB A C E

11 Depth-First Search11 Analysis of DFS Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice – once as UNEXPLORED – once as VISITED Each edge is labeled twice – once as UNEXPLORED – once as DISCOVERY or BACK Method incidentEdges is called once for each vertex DFS runs in O(n m) time provided the graph is represented by the adjacency list structure – Recall that v deg(v) 2m

12 DFS 1 2 adjacency matrix adjacency list 125315432

13 #include int map[5][5] = {{0,1,1,0,1},{1,0,1,0,1},{1,1,0,1,1},{0,0,1,0,1},{1,1,1,1,0}}; int ans[8] = {0}; void DFS(int idx, int now) { ans[idx] = now; if(idx == 8) { for(int i = 0; i < 9; i++){ printf("%d", ans[i]+1);} puts(""); return ; } int i=0; adjacency matrix

14 for(i = 0; i < 5; i++) { if(map[now][i] == 1) { map[now][i] = map[i][now] = 0; DFS(idx+1, i); map[now][i] = map[i][now] = 1; } }} int main() { DFS(0, 0); return 0;} adjacency matrix

15 DFS adjacency matrix N^2 O( v^2) recursive 240 step 6000 6000/24025(5^2)

16 #include #define MAXEDGE 8 #define MAXNODE 6 /* Node 0~5 */ typedef struct node { /* pos: edge position */ int pos, node; struct node *next; } Node; typedef enum {FALSE = 0, TRUE} bool; void init(int, int, int); void backtrack(int, int); void freeg(void); Node *g[MAXNODE] = {NULL}; bool visited[MAXEDGE]; char output[MAXEDGE + 1]; adjacency list

17 bool visited[MAXEDGE]; char output[MAXEDGE + 1]; int main(void) { int i; init(1, 2, 0); init(2, 1, 0); init(1, 3, 1); init(3, 1, 1); init(1, 5, 2); init(5, 1, 2); init(2, 3, 3); init(3, 2, 3); init(2, 5, 4); init(5, 2, 4); init(3, 4, 5); init(4, 3, 5); init(3, 4, 5); init(4, 3, 5); init(3, 5, 6); init(5, 3, 6); init(4, 5, 7); init(5, 4, 7); output[0] = 1 + '0'; backtrack(1, 1); freeg(); return 0;} adjacency list

18 void init(int v1, int v2, int pos) { Node *pt, *tmp; tmp = (Node *)malloc(sizeof(Node)); tmp->next = NULL; tmp->node = v2; tmp->pos = pos; if (g[v1] == NULL) { g[v1] = tmp; return; } pt = g[v1]; while (pt->next != NULL) pt = pt->next; pt->next = tmp; } /* backtrack: use backtracking method to output answer */ void backtrack(int index, int node) { Node *pt; int pos; if (index == MAXEDGE + 1) { output[index] = '\0'; printf("%s\n", output); return; } for (pt = g[node]; pt; pt = pt->next) { pos = pt->pos; node = pt->node; if (!visited[pos]) { visited[pos] = TRUE; output[index] = node + '0'; backtrack(index + 1, node); visited[pos] = FALSE; } /* freeg: free all graph node */ void freeg(void) { Node *pt, *tmp; int i; for (i = 0; i < MAXNODE; i++) { pt = g[i]; while (pt != NULL) { tmp = pt; pt = pt->next; free(tmp); } adjacency list

19 /* backtrack: use backtracking method to output answer */ void backtrack(int index, int node) { Node *pt; int pos; if (index == MAXEDGE + 1) { output[index] = '\0; printf("%s\n", output); return; } for (pt = g[node]; pt; pt = pt->next) { pos = pt->pos; node = pt->node; if (!visited[pos]) { visited[pos] = TRUE; output[index] = node + '0'; backtrack(index + 1, node); visited[pos] = FALSE; } } } /* freeg: free all graph node */ void freeg(void) { Node *pt, *tmp; int i; for (i = 0; i < MAXNODE; i++) { pt = g[i]; while (pt != NULL) { tmp = pt; pt = pt->next; free(tmp); } adjacency list

20 /* freeg: free all graph node */ void freeg(void) { Node *pt, *tmp; int i; for (i = 0; i < MAXNODE; i++) { pt = g[i]; while (pt != NULL) { tmp = pt; pt = pt->next; free(tmp); } adjacency list

21 adjacency list adjacency list O( E*logV) recursive 195 step 1200 1200/1956.1538…..

22 #include int main(void) { printf("123153452\n123154352\n123451352\n123453152\n 123513452\n123543152\n125134532\n125135432\n125315 432\n125345132\n125431532\n125435132\n132153452\n13 2154352\n132534512\n132543512\n134512352\n13451253 2\n134521532\n134523512\n134532152\n134532512\n1351 23452\n135125432\n135215432\n135234512\n135432152\n 135432512\n152134532\n152135432\n152345312\n152354 312\n153123452\n153125432\n153213452\n153254312\n15 3452132\n153452312\n154312352\n154312532\n15432135 2\n154325312\n154352132\n154352312\n"); return 0;}

23 printf, O(1)

24

25

26 44

27 O(V*V), list O(E*logV) O(1)


Download ppt "96703046 98207429 99703002 [UVa]291 - The House Of Santa Claus."

Similar presentations


Ads by Google