Informatik II Ubung 10 FS 2019 1 Program Today Repetition - - PowerPoint PPT Presentation

informatik ii
SMART_READER_LITE
LIVE PREVIEW

Informatik II Ubung 10 FS 2019 1 Program Today Repetition - - PowerPoint PPT Presentation

Informatik II Ubung 10 FS 2019 1 Program Today Repetition Lectures: Adjacency Lists 1 Breadth-First-Search BFS 2 In-Class-Exercise 3 2 Adjacency List class Graph { // G = (V,E) as adjacency list private int V; // number of vertices


slide-1
SLIDE 1

Informatik II

¨ Ubung 10

FS 2019

1

slide-2
SLIDE 2

Program Today

1

Repetition Lectures: Adjacency Lists

2

Breadth-First-Search BFS

3

In-Class-Exercise

2

slide-3
SLIDE 3

Adjacency List

class Graph { // G = (V,E) as adjacency list private int V; // number of vertices private ArrayList<LinkedList<Integer>> adj; // adj. list // Constructor public Graph(int n) { V = n; adj = new ArrayList<LinkedList<Integer>>(V); for (int i=0; i<V; ++i) adj.add(i,new LinkedList<Integer>()); } // Edge adder method public void addEdge(int u,int v) { adj.get(u).add(v); } }

3

slide-4
SLIDE 4

Adjacency List

Properties:

ArrayList

Get element in constant time.

LinkedList

Add element in constant time. Iterate over whole list in linear time.

4

slide-5
SLIDE 5

Adjacency List

Properties:

ArrayList

Get element in constant time.

LinkedList

Add element in constant time. Iterate over whole list in linear time.

  • addEdge(u,v) = adj.get(u).add(v) runs in constant time O(1).

4

slide-6
SLIDE 6

Adjacency List

Properties:

ArrayList

Get element in constant time.

LinkedList

Add element in constant time. Iterate over whole list in linear time.

  • addEdge(u,v) = adj.get(u).add(v) runs in constant time O(1).
  • for (int v : adj.get(u)) runs in time O(deg+(u)).

4

slide-7
SLIDE 7

Runtimes of simple Operations

Operation Matrix List Find neighbours/successors of v ∈ V find v ∈ V without neighbour/successor

(u, v) ∈ E ?

Insert edge Delete edge

5

slide-8
SLIDE 8

Runtimes of simple Operations

Operation Matrix List Find neighbours/successors of v ∈ V

Θ(n)

find v ∈ V without neighbour/successor

(u, v) ∈ E ?

Insert edge Delete edge

5

slide-9
SLIDE 9

Runtimes of simple Operations

Operation Matrix List Find neighbours/successors of v ∈ V

Θ(n) Θ(deg+ v)

find v ∈ V without neighbour/successor

(u, v) ∈ E ?

Insert edge Delete edge

5

slide-10
SLIDE 10

Runtimes of simple Operations

Operation Matrix List Find neighbours/successors of v ∈ V

Θ(n) Θ(deg+ v)

find v ∈ V without neighbour/successor

Θ(n2) (u, v) ∈ E ?

Insert edge Delete edge

5

slide-11
SLIDE 11

Runtimes of simple Operations

Operation Matrix List Find neighbours/successors of v ∈ V

Θ(n) Θ(deg+ v)

find v ∈ V without neighbour/successor

Θ(n2) Θ(n) (u, v) ∈ E ?

Insert edge Delete edge

5

slide-12
SLIDE 12

Runtimes of simple Operations

Operation Matrix List Find neighbours/successors of v ∈ V

Θ(n) Θ(deg+ v)

find v ∈ V without neighbour/successor

Θ(n2) Θ(n) (u, v) ∈ E ? Θ(1)

Insert edge Delete edge

5

slide-13
SLIDE 13

Runtimes of simple Operations

Operation Matrix List Find neighbours/successors of v ∈ V

Θ(n) Θ(deg+ v)

find v ∈ V without neighbour/successor

Θ(n2) Θ(n) (u, v) ∈ E ? Θ(1) Θ(deg+ v)

Insert edge Delete edge

5

slide-14
SLIDE 14

Runtimes of simple Operations

Operation Matrix List Find neighbours/successors of v ∈ V

Θ(n) Θ(deg+ v)

find v ∈ V without neighbour/successor

Θ(n2) Θ(n) (u, v) ∈ E ? Θ(1) Θ(deg+ v)

Insert edge

Θ(1)

Delete edge

5

slide-15
SLIDE 15

Runtimes of simple Operations

Operation Matrix List Find neighbours/successors of v ∈ V

Θ(n) Θ(deg+ v)

find v ∈ V without neighbour/successor

Θ(n2) Θ(n) (u, v) ∈ E ? Θ(1) Θ(deg+ v)

Insert edge

Θ(1) Θ(1)

Delete edge

5

slide-16
SLIDE 16

Runtimes of simple Operations

Operation Matrix List Find neighbours/successors of v ∈ V

Θ(n) Θ(deg+ v)

find v ∈ V without neighbour/successor

Θ(n2) Θ(n) (u, v) ∈ E ? Θ(1) Θ(deg+ v)

Insert edge

Θ(1) Θ(1)

Delete edge

Θ(1)

5

slide-17
SLIDE 17

Runtimes of simple Operations

Operation Matrix List Find neighbours/successors of v ∈ V

Θ(n) Θ(deg+ v)

find v ∈ V without neighbour/successor

Θ(n2) Θ(n) (u, v) ∈ E ? Θ(1) Θ(deg+ v)

Insert edge

Θ(1) Θ(1)

Delete edge

Θ(1) Θ(deg+ v)

5

slide-18
SLIDE 18

Breadth-First-Search BFS

BFS starting from a:

a b c d e f g h i

BFS-Tree: Distances and Parents

a b e d c f

distance 0

a

6

slide-19
SLIDE 19

Breadth-First-Search BFS

BFS starting from a:

a b c d e f g h i a

BFS-Tree: Distances and Parents

a b e d c f

distance 0 distance 1

a b d e a

6

slide-20
SLIDE 20

Breadth-First-Search BFS

BFS starting from a:

a b c d e f g h i a b

BFS-Tree: Distances and Parents

a b e d c f

distance 0 distance 1 distance 2

a b d e c a b

6

slide-21
SLIDE 21

Breadth-First-Search BFS

BFS starting from a:

a b c d e f g h i a b d

BFS-Tree: Distances and Parents

a b e d c f

distance 0 distance 1 distance 2

a b d e c a b d

6

slide-22
SLIDE 22

Breadth-First-Search BFS

BFS starting from a:

a b c d e f g h i a b d e

BFS-Tree: Distances and Parents

a b e d c f

distance 0 distance 1 distance 2

a b d e c f a b d e

6

slide-23
SLIDE 23

Breadth-First-Search BFS

BFS starting from a:

a b c d e f g h i a b d e c

BFS-Tree: Distances and Parents

a b e d c f

distance 0 distance 1 distance 2

a b d e c f a b d e c

6

slide-24
SLIDE 24

Breadth-First-Search BFS

BFS starting from a:

a b c d e f g h i a b d e c f

BFS-Tree: Distances and Parents

a b e d c f

distance 0 distance 1 distance 2

a b d e c f a b d e c f

6

slide-25
SLIDE 25

Quiz

In how many ways can the following directed graphs be topologically sorted each? A B C D number sortings ? A B C D number sortings ? A B C D number sortings ?

7

slide-26
SLIDE 26

Quiz

In how many ways can the following directed graphs be topologically sorted each? A B C D number sortings 2 A B C D number sortings 1 A B C D number sortings

7

slide-27
SLIDE 27

In-Class-Exercises: Route planning

Exercise: You are given a directed, unweighted Graph G = (V, E), represented by an adjacency list, and a designated node t ∈ V (e.g., an emergency exit). Design an algorithm, which computes for each node u ∈ V an outgoing edge in direction of a shortest path to t. and has a running time of O(|V | + |E|).

8

slide-28
SLIDE 28

In-Class-Exercises: Route planning

Solution:

1 Make a copy of the graph with edges having reverse direction:

GT = (V, ET), where ET = {(v, u) | (u, v) ∈ E}.

Running time: O(|V | + |E|).

2 Start a breadth-first search of GT, starting from t,

and store all edges of the BFS-Tree. Running time: O(|V | + |ET|) = O(|V | + |E|).

3 Assign the stored edges (in reverse direction)

to the discovered nodes. Running time: O(|V |).

9

slide-29
SLIDE 29

Questions / Suggestions?

10