Лекция 8. Графы. Обходы графов

Download Лекция 8. Графы. Обходы графов

Post on 20-Jul-2015

155 views

Category:

Software

2 download

Embed Size (px)

TRANSCRIPT

  • 8.

    E-mail: mkurnosov@gmail.comWWW: www.mkurnosov.net

    () , 2015

  • 2

    (graph)

    = , ,

    = , = ||,

    = 1,2, , , = { 1, 1 , 2, 2 , , (, )}

    = , , , ,

    = , , , , , , , , (, )

    (1, 5) (5, 1)

    vertex

    edge

  • 3

    (graphs)

    (undirected graphs)

    (edges)

    (directed graphs)

    (arcs, edges)

    (1, 2) (2, 1) (1, 3) (3, 1) !

  • 4

    (vertex, node)

    (adjacent vertices)

    (4, 6) (incident) 4 6

    (edge, link)

    (path) , ( ) ( )

    : (10, 8, 3, 5)

  • 5

    (cycle) , : (4, 6, 7, 8, 3, 4)

    (vertex degree) ,

    deg(7) = 2, deg(1) = 3

    (connected graph) ,

  • 6

    (weighted graph) , ()

    (, )

    34 = 5, 42 = 24,

  • 7

    (complete graph) , ( )

    :

    =( 1)

    2

    D (density):

    =2

    ( 1)

    D = 1 =

    4

    4 32

    =4

    6= 0.66

  • 8

    (dense graph) ,

    = ( 2)

    =219

    76= 0.9, > 0.5

    (sparse graph) ,

    = (||)

    =27

    76= 0.33, < 0.5

  • 9

    ( )

    :

    (adjacency matrix)

    (adjacency list)

  • 10

    A (adjacency matrix) n n ,

    = 1, , ,0, .

    ( 2)

    (, )

    O(1)

    (|E| |V|2)

  • 11

    8 ?

    int a[n][n];

  • 12

    8 ?

    int a[n][n];

    sizeof(int) = 4

    8 = 8 230

    8 230 / 4 = 2 230 231 = 2 147 483 648 int

    = 231 = 46340

    int a[46340][46340];

    , (, 90% (~ 7 ), n = 43 347)

    unsigned char uint8_t?

  • 13

    (adjacency list) A[n], A[i] i

    (|E| |V|)

  • 14

    A[n + 1] L[2m]

    i:L[A[i]], L[A[i] + 1], , L[A[i + 1] 1]

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    2 4 1 3 5 6 2 4 6 1 3 2 2 3 6

    1 2 3 4 5 6 7

    1 3 7 10 12 13 15A:

    L:

    L

  • 15

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    2 4 1 3 5 6 2 4 6 1 3 2 2 3 6

    1 2 3 4 5 6 7

    1 3 7 10 12 13 15A:

    L:

    L

    // ifor (j = A[i]; j < A[i + 1]; j++) {

    v = L[j];// v

    }

    i: A[i + 1] A[i]

    - !

    T = O(m)

  • 16

    ( )

    ?

    ?

    20

  • (graph traversal) ()

    17

  • (depth-first search)

    18

    (depth-first search DFS) v

    ()

    function DFS(v)

    visited[v] = true

    // v

    for each u in Adj(v) do //

    if visited[u] = false then

    DFS(u) // u

    end if

    end for

    end function

  • (depth-first search)

    19

    2: DFS(2)

    for each u in Adj(2) do...

    end for

    , , O(|V|2)

    , , O(|V| + |E|)

  • (depth-first search)

    20

    2: DFS(2)

    for each u in Adj(2) do...

    end for

    , , O(|V|2)

    , , O(|V| + |E|)

  • (depth-first search)

    21

    2: DFS(2)

    for each u in Adj(2) do...

    end for

    , , O(|V|2)

    , , O(|V| + |E|)

  • (depth-first search)

    22

    2: DFS(2)

    for each u in Adj(2) do...

    end for

    , , O(|V|2)

    , , O(|V| + |E|)

  • (depth-first search)

    23

    2: DFS(2)

    for each u in Adj(2) do...

    end for

    , , O(|V|2)

    , , O(|V| + |E|)

    5

    3

  • (depth-first search)

    24

    2: DFS(2)

    for each u in Adj(2) do...

    end for

    , , O(|V|2)

    , , O(|V| + |E|)

  • (depth-first search)

    25

    2: DFS(2)

    for each u in Adj(2) do...

    end for

    , , O(|V|2)

    , , O(|V| + |E|)

  • (depth-first search)

    26

    2: DFS(2)

    for each u in Adj(2) do...

    end for

    , , O(|V|2)

    , , O(|V| + |E|)

    6

    7, 8

  • (depth-first search)

    27

    2: DFS(2)

    for each u in Adj(2) do...

    end for

    , , O(|V|2)

    , , O(|V| + |E|)

  • (depth-first search)

    28

    2: DFS(2)

    for each u in Adj(2) do...

    end for

    , , O(|V|2)

    , , O(|V| + |E|)

  • (breadth-first search)

    29

    (breadth-first search BFS, ) v

    ()

  • function BFS(v)

    visited[v] = true

    // v

    QueueEnqueue(v) // v

    while QueueSize() > 0 do

    u = QueueDequeue() //

    for each x in Adj(u) do

    if visited[x] = false then

    QueueEnqueue(x)

    visited[x] = true

    // x

    end if

    end for

    end while

    end function

    (breadth-first search)

    30

  • (breadth-first search)

    31

    2: BFS(2)

    : 2

    :1, 3, 5

    , , O(|V|2)

    , , O(|V| + |E|)

  • (breadth-first search)

    32

    2: BFS(2)

    : 1

    :3, 5, 4

    , , O(|V|2)

    , , O(|V| + |E|)

  • (breadth-first search)

    33

    2: BFS(2)

    : 3

    :5, 4, 8

    , , O(|V|2)

    , , O(|V| + |E|)

  • (breadth-first search)

    34

    2: BFS(2)

    : 5

    :4, 8

    , , O(|V|2)

    , , O(|V| + |E|)

  • (breadth-first search)

    35

    2: BFS(2)

    : 4

    :8, 6

    , , O(|V|2)

    , , O(|V| + |E|)

  • (breadth-first search)

    36

    2: BFS(2)

    : 8

    :6, 7, 9, 10

    , , O(|V|2)

    , , O(|V| + |E|)

  • (breadth-first search)

    37

    2: BFS(2)

    : 6

    :7, 9, 10

    , , O(|V|2)

    , , O(|V| + |E|)

  • (breadth-first search)

    38

    2: BFS(2)

    : 7

    :9, 10

    , , O(|V|2)

    , , O(|V| + |E|)

  • (breadth-first search)

    39

    2: BFS(2)

    : 9

    :10

    , , O(|V|2)

    , , O(|V| + |E|)

  • (breadth-first search)

    40

    2: BFS(2)

    : 10

    :

    , , O(|V|2)

    , , O(|V| + |E|)

  • 41

    #include

    #include

    #include "queue_array.h"

    struct graph {

    int nvertices; /* */

    int *m; /* n x n */

    int *visited;

    };

  • struct graph *graph_create(int nvertices){

    struct graph *g;

    g = malloc(sizeof(*g));g->nvertices = nvertices;g->visited = malloc(sizeof(int) * nvertices);g->m = malloc(sizeof(int)

    * nvertices * nvertices);graph_clear(g); // , O(n^2)return g;

    }

    42

    TCreate = O(n2)

    Memory = O(n2)

  • 43

    void graph_clear(struct graph *g){

    int i, j;

    for (i = 0; i < g->nvertices; i++) {g->visited[i] = 0;for (j = 0; j < g->nvertices; j++) {

    g->m[i * g->nvertices + j] = 0;}

    }} TClear = O(n

    2)

  • 44

    void graph_free(struct graph *g){

    free(g->m);free(g);

    }

  • /* * graph_set_edge: (i, j) w* i, j = 1, 2, ..., n*/

    void graph_set_edge(struct graph *g, int i, int j, int w)

    {g->m[(i - 1) * g->nvertices + j - 1] = w;g->m[(j - 1) * g->nvertices + i - 1] = w;

    }

    int graph_get_edge(struct graph *g, int i, int j){

    return g->m[(i - 1) * g->nvertices + j - 1];}

    45

  • void graph_dfs(struct graph *g, int v)

    {

    int i;

    g->visited[v - 1] = 1;

    printf("Vertex %d\n", v);

    for (i = 0; i < g->nvertices; i++) {

    if (g->m[(v - 1) * g->nvertices + i] > 0

    && g->visited[i] == 0)

    {

    graph_dfs(g, i + 1);

    }

    }

    }

    (DFS)

    46

    TDFS = O(n2)

  • (BFS)

    47

    void graph_bfs(struct graph *g, int v){

    int i, j;struct queue *q;

    for (i = 0; i < g->nvertices; i++)g->visited[i] = 0;

    // q = queue_create(g->nvertices);

    // g->visited[v - 1] = 1;printf("Vertex %d\n", v);queue_enqueue(q, v - 1);

  • (BFS, )

    48

    while (queue_size(q) > 0) {

    i = queue_dequeue(q);

    for (j = 0; j < g->nvertices; j++) {

    if (g->m[i * g->nvertices + j] > 0

    && g->visited[j] == 0)

    {

    queue_enqueue(q, j);

    g->visited[j] = 1;

    printf("Vertex %d\n", j + 1);

    }

    }

    }

    queue_free(q);

    } TBFS = O(n2)

  • 49

    int main(){

    struct graph *g;

    g = graph_create(10);graph_set_edge(g, 1, 2, 1);graph_set_edge(g, 1, 4, 1);/* ... */

    printf("DFS:\n");graph_dfs(g, 2);

    printf("BFS:\n");graph_bfs(g, 2);

    graph_free(g);return 0;

    }

  • 50

    BFS DFS [Aho, C. 217], [CLRS, . 630, . 639]

    Compressed Sparse Row Graph (CSR) http://www.boost.org/doc/libs/1_45_0/libs/graph/doc/compressed_sparse_row.html

    Sparse matrix http://en.wikipedia.org/wiki/Sparse_matrix

Recommended

View more >