6 graph

30
1 204212 : Module6 Graph 204212 : Module6 Graph 1 1 6.1 Graph - A directed graph(or digraph) G : is a pair(V,E), where V is a finite set and E is binary relation on V. ไไไไไไ G : ไไไไไไไไไไไไไไไ (V,E) ไไไ V ไไไไ ไไไไไไไไไไไไไไไไไไไ ไไไ E ไไไไ ไไไไไไไไไไไไไไไไไไไไไ ไไ V - The set V is called the vertex set of G, and its elements are called vertices. ไไไไ V ไไไไไไไไไไไ ไไไไไไไไไไไไไ ไไไ G ไไไ ไไไไไไไไไไไไไไไไไไไไไไไไ ไไไไไ ไไไไไ - The set E is called the edge set of G, and its elements are called edges. ไไไไ E ไไไไไไไไไไไไไไ ไไไไไไไ G ไไไ ไไไไไไไไไไไไไไไไไไไไไไไไ edges

Upload: totiz

Post on 14-Nov-2014

102 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6 Graph

1

204212 : Module6 – Graph204212 : Module6 – Graph 11

6.1 Graph- A directed graph(or digraph) G : is a pair(V,E), where V is a finite set and E is binary relation on V.ไดกราฟ G : คือคื��ลำ�าด�บของ (V,E) โดย V เป็�น เซ็�ตจำ�าก�ดเซ็�ทหน� ง แลำะ E เป็�นคืวามสั�มพั�นธ์(แบบไบนาร) บน V- The set V is called the vertex set of G, and its elements are called vertices.เซ็�ท V ถู�กเร)ยกว�า เวอร(เท�กเซ็�ท ของ G แลำะ สัมาชิ,กของเซ็�ทน)-เร)ยกว�า เวอร(ท,ซ็สั(- The set E is called the edge set of G, and its elements are called edges.เซ็�ท E ถู�กเร)ยกว�าเอจำ เซ็�ทของ G แลำะสัมาชิ,กของเซ็�ทน)-เร)ยกว�า edges

Page 2: 6 Graph

2

204212 : Module6 – Graph204212 : Module6 – Graph 22

- self-loops : edges from a vertex to itself. คือ edges จำากเวอร(เท�กอ�นหน� งไป็ย�งต�วม�นเอง - An undirected graph G = (V,E) , the edge set E

consists of unordered pairs of vertices, rather than ordered pairs. That is, an edge is a set{u,v}, where u,v V and u ≠ v undigraph คือกราฟท) เซ็�ทของ เอจำ E ป็ระกอบด.วย เวอร(ท,ซ็ท) ไม�ม)ลำ�าด�บ

- an edge (u,v) are considered to be the same edge.เอจำ (u,v) ถูอว�าเป็�นเอจำเด)ยวก�น

- Undigraph self loop are forbidden, and so every edge consists of exactly two distinct vertices.

Page 3: 6 Graph

3

204212 : Module6 – Graph204212 : Module6 – Graph 33

- incident digraph : if (u,v) is an edge in a directed graph G=(V,E), we say that(u,v) is incident from or leaves vertex u and is incident to or enters vertex v.อ,นซ็,เด�นท(ของไดกราฟ ถู.าเอจำ (u,v) เป็�นเอจำ ของไดกราฟกราฟ G อ,นซ็)เดนฟอร(ม คือ edges ท) ออกจำากเวอร(เท�กซ็( u อ,นซ็)เดนท(ท�คือ เอจำท) เข.าหาเวอร(เท�กซ็( v

undigraph : if (u,v) is an edge in a directed graph G=(V,E), we say that(u,v) is incident on vertices u and v

Page 4: 6 Graph

4

204212 : Module6 – Graph204212 : Module6 – Graph 44

- adjacentDigraph : if (u,v) is an edge in a graph G=(V,E),

we say that v is adjacent to vertex u. If v is adjacent to u denote by u -> v

เอจำ (u,v) เราเร)ยกว�า v เป็�น adjacent เวอร(เท�กซ็( u

Undigraph : adjacent relation is symmetric.

- Degree Undigraph : is the number of edges incident on

it.Digraph : in degree (number of edges entering

it.), out degree

Page 5: 6 Graph

5

204212 : Module6 – Graph204212 : Module6 – Graph 55

- path of length k from a vertex u to a vertex u’ in a graph G=(V,E) is a sequence (vo,v1,..,vk) of vertices such that u = v0, u’ = vk, and (vi-1, vi) E for I = 1, 2, .., k

Page 6: 6 Graph

6

204212 : Module6 – Graph204212 : Module6 – Graph 66

โครงสร�างข้�อมู�ลที่��ใช้�เก็�บข้�อมู�ลในร�ปแบบก็ราฟค�อ1. Adjacency list

2. Adjacency Matrixโดยโคืรงสัร.างข.อม�ลำท�-ง 2 แบบน)-จำะสัามารถูเก�บกราฟได.ท�-ง

digraph แลำะ undigraph

Page 7: 6 Graph

7

204212 : Module6 – Graph204212 : Module6 – Graph 77

6.2 Breadth-first search : Algorithm for searching a graph

Given a graph G = (V,E) and distinguished source vertex s, bfs systematically explores the edges of G to discover every vertex that is reachable from s to all such reachable vertices.Output – Compute distance

- Produces a bfs tree.

Page 8: 6 Graph

8

204212 : Module6 – Graph204212 : Module6 – Graph 88

BFS(G,s) for each vertex u E V[G] – {s} do pass[u] = 0

d[u] = -1pred[u] = -1

pass[s] =1 d[s] =0 pred[s] =s

Page 9: 6 Graph

9

204212 : Module6 – Graph204212 : Module6 – Graph 99

Enqueue(s);while (queuesize() != 0) { u = head(q) node=adj[u];

while( node!= NULL){ v= node->value;

if (pass[v]=0){ pass[v] = 1

d[v] = d[u]+1pred[v] = uenqueue(v)

}node=node->next;

}dequeue()

}

Page 10: 6 Graph

10

204212 : Module6 – Graph204212 : Module6 – Graph 1010

6.3 DFS DFS จำ/ดป็ระสังคื( คื.นหาข.อม�ลำตามคืวามลำ�กของกราฟ โดยหา

เสั.นทางท) ลำ�กท) สั/ดจำากการเด,นจำาก เวอร(เท�กซ็( u ไป็ย�ง เวอร(เท�กซ็( v ซ็� งเป็�นเวอร(เท�กซ็(ข.างเคื)ยงท) ย�งไม�ได.เด,นผ่�าน ท�าเชิ�นน)-จำนกระท� งไม�ม) edge ท) สัามารถูเด,นต�อไป็ได.อ)ก จำากน�-นจำ�งท�าการเด,นกลำ�บ (backtrack) ไป็ย�ง source

- Edges are explored out of the most recently discovered vertex v that still has unexplored edge leaving it.

- When all of v’s edges have been explored, the search backtracks to explore edges leaving the vertex from which v has discovered. Until we have discovered all the vertices that are reachable from the original source vertex.

Page 11: 6 Graph

11

204212 : Module6 – Graph204212 : Module6 – Graph 1111

DFS treess t| / \ z v u

/ \ y w

| x

Page 12: 6 Graph

12

204212 : Module6 – Graph204212 : Module6 – Graph 1212

DFS (G) for ( u=0;u< 6; u++) { pass[u]=0;

pred[u]=-1; } time=0; for ( u=0;u<6;u++) { if (pass[u]=0)

{ DFS_Visit(u)}

}

Page 13: 6 Graph

13

204212 : Module6 – Graph204212 : Module6 – Graph 1313

time=0 เป็�น global DFS_Visit(u) pass[u]=1 d[u]=++time; for each v adj[u] { if (pass[v] == 0)

{ pred[v] = uDFS_Visit(v)

} } pass[u]=1 f[u]=++time;

Page 14: 6 Graph

14

204212 : Module6 – Graph204212 : Module6 – Graph 1414

6.3.1 Topological sort Topological sort of a DAG G=(V,E) is a linear

ordering of all its vertices such that if G contains an edge(u,v) , then u appears before v in the ordering. ( If the graph is not acyclic, then no linear ordering is possible.)

Topological sort คือการน�าเวอร(ท,ซ็ของกราฟกราฟหน� งมา

เร)ยงเป็�นเสั.นตรงแบบม)ลำ�าด�บ โดยม)ข.อก�าหนดว�ากราฟน�-นจำะต.องม)คื/ณสัมบ�ต,เป็�น directed acyclic graph หรอ DAG (คือกราฟท) ไม�ม)ไซ็เคื,ลำ)

Page 15: 6 Graph

15

204212 : Module6 – Graph204212 : Module6 – Graph 1515

Algorithm Topological_sort(G) 1. call DFS(G) to computer finishing times f[v]

for each vertex v. - ท�า DFS กราฟ 2. as each vertex is finished, insert it onto the

front of a linked list- เม อ DFS แลำ.ว ให.น�า vertex ท) ม) finish time

สั�งสั/ดใสั�ไว.ต�าแหน�งแรกของ linklist finish time ท) ม)คื�ารองลำงมาให.น�าไป็ใสั�ไว.ต�าแหน�งท) 2 ของ linklist แลำะท�าเชิ�นน)-โดยเร)ยงจำากมากไป็น.อย

3. return the link list of vertices.

Page 16: 6 Graph

16

204212 : Module6 – Graph204212 : Module6 – Graph 1616

6.3.2 Strongly connected components- A digraph is strongly connected if every two vertices are reachable from each other. The strongly connected components of a graph are the equivalence classes of vertices under the “are mutually reachable” relation.

- ไดกราฟ เป็�น strongly connected เม อม)เวอร(ท,ซ็ 2เวอร(ท,ซ็สัามารถูถู�งก�นได.ท�-ง 2 ด.าน

- strongly connected components ของกราฟหน� งคือ ม)เวอร(ท,ซ็ ท)ม)คืวามสั�มพั�นธ์( are mutually reachable

Page 17: 6 Graph

17

204212 : Module6 – Graph204212 : Module6 – Graph 1717

Strongly-Connected-Component Alogrithm 1. call DFS(G) to compute finishing times f[u]

for each vertex u- ท�า DFS G

2. compute GT

- ท�าการ transpose graph 3. call DES(GT), but the main loop of DFS,

consider the vertices in order of decreasing f[u] (as computed in line 1)- ท�า DFS GT โดยเร, มจำาก fished time ท) ม)คื�าสั�งสั/ด

4. output the vertices of each tree

Page 18: 6 Graph

18

204212 : Module6 – Graph204212 : Module6 – Graph 1818

Input and Output In Cprintf :writes formatted output to standard

outputsprintf : writes formatted output to a buffer scanf : reads formatted data from

standard outputsscanf : reads formatted data from a buffergetchar : reads a character from standardinputputchar : writes a character to standard outputgets : read a line from a standard

inputputs : writes a line to standard output,

followed by a newline

Page 19: 6 Graph

19

204212 : Module6 – Graph204212 : Module6 – Graph 1919

#include <stdio.h>int main(){ char buf[100], *cmd = "pr -w“; char *file = "/tmp/data"; int width = 90; sprintf(buf, "%s %d %s",cmd,width,file); printf("%s\n",buf); system("pause");}

Page 20: 6 Graph

20

204212 : Module6 – Graph204212 : Module6 – Graph 2020

I/O with filesfopen : open a file with a specified

modefclose : close a filefprintf : writes formatted output to a filefscanf : reads formatted data from a filefgetc : reads a character from a filegetc : reads a character to a filefputc : writes a character to a fileputc : writes a character to a fileungetc : unread a character from a filegetw : reads a word from a file

Page 21: 6 Graph

21

204212 : Module6 – Graph204212 : Module6 – Graph 2121

putw : writes a word to a filefgets : reads a fie from a filefputs : writes a file to a filefread : reads data from a filefwrite : write data to a file

Page 22: 6 Graph

22

204212 : Module6 – Graph204212 : Module6 – Graph 2222

fopen access modes r Read from the file w Write to the file; if the fie already

exists, its previous contents are lost; if file doesn’t exist, it’s created

a Read and write to the end of file; if file doesn’t exist, it’s created

r+ Read and write to the file ( like r, but data can also be written to the file)

w+ Read and write to the file (works like w, but data can also be read from the file)

a+ Read and write to the file ( works like a -- writes can only go to the end of the file , but reading also permitted anywhere in the file)

Page 23: 6 Graph

23

204212 : Module6 – Graph204212 : Module6 – Graph 2323

#include <stdio.h>int main(){ FILE *outfile; outfile = fopen("d:\data.dat","w"); fprintf(outfile,"Bob\n"); fprintf(outfile,"Bill\n"); fclose(outfile);}

Page 24: 6 Graph

24

204212 : Module6 – Graph204212 : Module6 – Graph 2424

fprintf(stderr,”Couldn’t open file \n”);fprintf(stdout,”Hello\n”);

File *infile;infile = fopen(”d:\number.dat”,”r”);fscanf(infile,”%d %d”,&x, &y);

Page 25: 6 Graph

25

204212 : Module6 – Graph204212 : Module6 – Graph 2525

Example Copy file#include <stdio.h>int main(){ FILE *infile, *outfile; int c; if((infile = fopen("d:\data.dat","r"))

== (FILE*)NULL) { fprintf(stderr,"Can't read names\n"); exit(1); }

Page 26: 6 Graph

26

204212 : Module6 – Graph204212 : Module6 – Graph 2626

if((outfile = fopen("d:\data.out","w"))==(FILE *)NULL)

{ fprintf(stderr,"Can't write names\n"); exit(2); } while( (c=getc(infile)) != EOF) putc(c,outfile);}

Page 27: 6 Graph

27

204212 : Module6 – Graph204212 : Module6 – Graph 2727

getw(infile);fgets(buf, 81, infile);fputs(buf,outfile);

Note fgets stores a null character at the end of the array, fgets will read less than 80 characters if it reaches the end of the file or if it reads a newline character.

Page 28: 6 Graph

28

204212 : Module6 – Graph204212 : Module6 – Graph 2828

Random I/OFILE *fp;fp = fopen(“d:\data.dat”,”r”);long offset;offset = ftell(fp);long offset;int origin;fseek(fp,offset, origin);/* SEEK_SET Beginning of ile *//* SEEK_CUR Current position *//* SEEK_END End of file */

Page 29: 6 Graph

29

204212 : Module6 – Graph204212 : Module6 – Graph 2929

Example#include <stdio.h>int main(){ FILE *fp; int where, c; long i = 0L; fp=fopen("d:\data.dat","r"); where = ftell(fp); printf("%d\n",where); fseek(fp,2L,SEEK_SET); where = ftell(fp); c=getc(fp); printf("%d %c\n",where,c);

Page 30: 6 Graph

30

204212 : Module6 – Graph204212 : Module6 – Graph 3030

rewind(fp); where = ftell(fp); c=getc(fp); printf("%d %c\n",where,c); system("pause");}

BobBill

02 b0 BPress any key to continue . . .