Download presentation

Presentation is loading. Please wait.

Published byBailey McKay Modified over 2 years ago

1
[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

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

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

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 /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 / …..

22
#include int main(void) { printf(" \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n"); return 0;}

23
printf, O(1)

24

25

26
44

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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google