Lecture 11 Graph algorithms: Order in acyclic graphs - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Lecture 11 Graph algorithms: Order in acyclic graphs

Description:

Directed Acyclic Graphs (DAG) ... In DAG there is a node with no incoming edges ... After removing node still a DAG remains: ... – PowerPoint PPT presentation

Number of Views:133
Avg rating:3.0/5.0
Slides: 11
Provided by: DarekKo5
Category:

less

Transcript and Presenter's Notes

Title: Lecture 11 Graph algorithms: Order in acyclic graphs


1
Lecture 11 Graph algorithmsOrder in acyclic
graphs
  • COMP 523 Advanced Algorithmic Techniques
  • Lecturer Dariusz Kowalski

2
Overview
  • Previous lectures
  • Testing bipartiteness
  • Testing for (directed) cycles
  • This lecture
  • Ordering nodes in acyclic directed graphs

3
How to represent directed graphs?
  • Given directed graph G (V,E) , how to represent
    it?
  • Adjacency matrix ith node is represented as ith
    row and ith column, edge from ith to jth node is
    represented as 1 in row i and column j, (0 if
    there is no directed edge from i to j)
  • Adjacency list nodes are arranged as array/list,
    each node record has two lists one stores
    in-neighbours other stores out-neighbours

1
2
3
4
1
1
2
3
4
4
0
1
1
1
1
2
3
1
2
0
0
1
0
4
3
1
2
4
2
3
0
0
0
0
4
1
3
1
4
1
0
1
0
3
Adjacency matrix
Adjacency list
4
Directed Acyclic Graphs (DAG)
  • Directed graph G (edges have directions - one end
    is the beginning, the other one is the end of the
    edge).
  • Reversed graph Gr has the same nodes but each
    edge is a reversed edge from graph G.
  • Test if given direct graph is acyclic (DAG) last
    lecture, directed DFS approach in time and memory
    O(mn).
  • Problem is it possible to order all nodes
    topologically, which is if (v,w) is a directed
    edge in G then v is before w?
  • Note there may be many topological orders.

graph G
reversed graph Gr
5
Opposite property
  • Fact
  • If graph G has a topological order then it is
    DAG.
  • Proof
  • Suppose, to the contrary, that G has topological
    order and it also has a cycle. Let v be the
    smallest element in the cycle according to the
    topological order. It follows that its successor
    in the cycle must be smaller than v, but this
    contradicts the fact that v is the smallest.
    Hence G must not have a cycle.

6
Key property
  • Property
  • In DAG there is a node with no incoming edges
  • After removing the node with the incident edges,
    remaining graph is still DAG
  • Proof
  • Node with no incoming edges
  • Suppose, to the contrary, that every node has at
    least one incoming edge. It follows that starting
    from any node we can always leave a node by
    incoming edge (in opposite direction) until we
    return to a visited node, which makes a cycle.
    This is a contradiction.
  • After removing node still a DAG remains
  • There could not be a cycle in smaller graph,
    since it would be in the original one.

7
Algorithm for topological order
  • Algorithm
  • Repeat until all nodes are in the ordered list
  • Find a node with no incoming edges
  • Remove this node and all its outgoing edges
  • Efficient implementation
  • Preprocessing
  • for each node keep info if it is active or not
    (additional array Active of size n needed) - at
    the beginning all nodes are active
  • go through the list and for every node store the
    number of incoming edges from other active nodes
    (additional array Counter of size n needed)
  • select on list S those nodes which have no
    incoming edges.
  • During the algorithm
  • While removing the node (make inactive), go
    through its outgoing active neighbours and
    decrease a counter of the active out-neighbours
    by one - if it becomes zero put this active node
    to list S
  • Node with no incoming edges is always taken (and
    then removed) from list S.

3
3
4
2
2
2
1
1
1
1
8
Complexity
  • Time O(mn)
  • Preprocessing
  • Initialization of array Active O(n)
  • Filling array Counter - counting edges O(mn)
  • Initialization of list S - checking array
    Counter O(n)
  • During the algorithm
  • While removing the node - go through its
    out-neigbours total O(mn)
  • Selecting from list S total O(1)
  • Memory O(mn)
  • Adjacency list O(mn)
  • Arrays Active and Counter, list S O(n)

9
Conclusions
  • Ordering nodes in acyclic graph in time and
    memory
  • O(m n).

10
Textbook and Exercises
  • Section 3.6
  • Prove that if a DFS and BFS trees in graph G are
    the same, than G is also the same like they (does
    not contain any other edge).
  • Prove that if G has n nodes, where n is even, and
    each node has at least n/2 neighbours then G is
    connected.
  • Prove the property of layers every edge is
    either between two consecutive layers or within
    one layer (two ends are in the same layer).
  • Design and analyse time and memory taken by BFS
    for directed graphs.
  • What happens if you try to use a different method
    of searching for checking bipartiteness or
    acyclicity?
Write a Comment
User Comments (0)
About PowerShow.com