directed vs undirected graph

Upload: marilyn-low

Post on 14-Apr-2018

253 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Directed vs Undirected Graph

    1/23

    1

    Directed and Undirected Graphs

    A graph is a mathematical structure consisting of a set of vertices and a setof edges connecting the vertices.

    Formally: G = (V,E), where V is a set and E V V.

    G = (V,E) undirected if for all v,w V:

    (v,w) E (w, v) E.

    Otherwise directed.

  • 7/30/2019 Directed vs Undirected Graph

    2/23

    2

    A directed graph

    G = (V,E) with vertex set

    V=

    0, 1, 2, 3, 4, 5, 6

    and edge set

    E=

    (0, 2), (0, 4), (0, 5), (1, 0), (2, 1), (2, 5), (3, 1), (3, 6), (4, 0), (4, 5), (6, 3), (6, 5).

    4 5 6

    32

    0 1

  • 7/30/2019 Directed vs Undirected Graph

    3/23

    3

    An undirected graph

    4

    0

    1

    2

    3

    6

    5

    7

    8

    9

    1011

    12

  • 7/30/2019 Directed vs Undirected Graph

    4/23

    4

    Examples of graphs in real lifeRoad Maps.Edges represent streets and vertices represent crossings.

  • 7/30/2019 Directed vs Undirected Graph

    5/23

    5

    Examples (contd)

    Airline route maps.

    Vertices represent airports, and there is an edge from vertexA

    to vertexB

    ifthere is a direct flight from the airport represented by A to the airport representedby B.

  • 7/30/2019 Directed vs Undirected Graph

    6/23

    6

    Examples (contd)Electrical Circuits.Vertices represent diodes, transistors, capacitors, switches, etc., and edges

    represent wires connecting them.

  • 7/30/2019 Directed vs Undirected Graph

    7/23

    7

    Examples (contd)

    Computer Networks.Vertices represent computers and edges represent network connections (cables)

    between them.

    The World Wide Web.Vertices represent webpages, and edges represent hyperlinks.

  • 7/30/2019 Directed vs Undirected Graph

    8/23

    8

    Examples (contd)

    Flowcharts.Vertices represent boxes and edges represent arrows.

    Conflict graphs in scheduling problems.Example: Assignment of time slots to exams. No overlapping time slots for examstaken by the same students.

    Modeled by graph whose vertices represent the exams, with an edge between twovertices if there is a student who has to take both exams.

  • 7/30/2019 Directed vs Undirected Graph

    9/23

    9

  • 7/30/2019 Directed vs Undirected Graph

    10/23

    10

    Examples (contd)

    Molecules.Vertices are atoms, edges are bonds between them.

    H C O H

    H

    C

    H

    H

    H

  • 7/30/2019 Directed vs Undirected Graph

    11/23

    11

    Examples (contd)

    Binary Relations in Mathematics.

    For example, the is a proper divisor relation on the integers.

    15

    10 2

    4

    8

    6

    7

    3 9

  • 7/30/2019 Directed vs Undirected Graph

    12/23

    12

    The adjacency matrix data structure

    Let G = (V,E) be a graph with n vertices. Vertices ofG numbered 0, . . . , n 1.

    The adjacency matrix ofG is the n n matrix A = (aij)0i,jn1 with

    aij =

    1 if there is an edge from vertex i to vertex j

    0 otherwise.

  • 7/30/2019 Directed vs Undirected Graph

    13/23

    13

    Example

    4 5 6

    32

    0 1

    0 0 1 0 1 1 01 0 0 0 0 0 00 1 0 0 0 1 0

    0 1 0 0 0 0 11 0 0 0 0 1 00 0 0 0 0 0 00 0 0 1 0 1 0

  • 7/30/2019 Directed vs Undirected Graph

    14/23

    14

    The adjacency list data structure

    Array with one entry for each vertex v, which is a list of all vertices adjacent to v.

    Example

    4 5 6

    32

    0 10

    1

    2

    4

    3

    5

    6

    2 5 4

    0

    1 5

    1 6

    0 5

    5 3

  • 7/30/2019 Directed vs Undirected Graph

    15/23

    15

    Adjacency matrices vs adjacency lists

    n = number of vertices, m = number of edges

    adjacency matrix adjacency list

    Space (n2) (n + m)

    Time to check if

    w adjacent to v

    (1) (out-degree(v))

    Time to visit allw adjacent to v

    (n) (out-degree(v))

    Time to visit alledges (n

    2

    ) (n + m)

  • 7/30/2019 Directed vs Undirected Graph

    16/23

    16

    Sparse and dense graphs

    G = (V,E) graph with n vertices and m edges

    Observation: m n2

    G dense ifm close to n2

    G sparse ifm much smaller than n2

  • 7/30/2019 Directed vs Undirected Graph

    17/23

    17

    Graph traversalsA traversal is a strategy for visiting all vertices of a graph.

    BFS = breadth-first search

    DFS = depth-first search

    General strategy:

    1. Let v be an arbitrary vertex

    2. Visit all vertices reachable from v

    3. If there are vertices that have not been visited, let v be such a vertex and goback to (2)

  • 7/30/2019 Directed vs Undirected Graph

    18/23

    18

    BFS

    Visit all vertices reachable from v in the following order:

    v

    all neighbours ofv

    all neighbours of neighbours ofv that have not been visited yet

    all neighbours of neighbours of neighbours ofv that have not been visited yet

    etc.

  • 7/30/2019 Directed vs Undirected Graph

    19/23

    19

    BFS (contd)

    Algorithm bfs(G)

    1. Initialise Boolean array visited by setting all entries to false

    2. Initialise QueueQ

    3. for all v V do

    4. if visited[v] = false then

    5. bfsFromVertex(G, v

    )

  • 7/30/2019 Directed vs Undirected Graph

    20/23

    20

    BFS (contd)Algorithm bfsFromVertex(G, v)

    1. visited[v] = true

    2. Q.enqueue(v)

    3. while not Q.isEmpty() do

    4. v Q.dequeue()

    5. for all w adjacent to v do

    6. if visited[w] = false then

    7. visited[w] = true

    8. Q.enqueue(w)

  • 7/30/2019 Directed vs Undirected Graph

    21/23

    21

    DFS

    Visit all vertices reachable from v in the following order:

    v

    some neighbor w ofv that has not been visited yet

    some neighbor x ofw that has not been visited yet

    etc., until the current vertex has no neighbour that has not been visited yet

    Backtrack to the first vertex that has a yet unvisited neighbour v.

    Continue with v

    , a neighbour, a neighbour of the neighbour, etc., backtrack,etc.

  • 7/30/2019 Directed vs Undirected Graph

    22/23

    22

    DFS (contd)

    Algorithm dfs(G)

    1. Initialise Boolean array visited by setting all entries to false

    2. Initialise Stack S

    3. for all v V do

    4. if visited[v] = false then

    5. dfsFromVertex(G, v)

  • 7/30/2019 Directed vs Undirected Graph

    23/23

    23

    DFS (contd)

    Algorithm dfsFromVertex(G, v)

    1. S.push(v)

    2. while not S.isEmpty() do

    3. v S.pop()

    4. if visited[v] = false then

    5. visited[v] = true

    6. for all w adjacent to v do

    7. S.push(w)