linked lists1 part-b3 linked lists. linked lists2 singly linked list (§ 4.4.1) a singly linked list...

37
Linked Lists 1 Part-B3 Linked Lists

Upload: porter-fann

Post on 30-Mar-2015

246 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 1

Part-B3

Linked Lists

Page 2: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 2

Singly Linked List (§ 4.4.1)A singly linked list is a concrete data structure consisting of a sequence of nodesEach node stores

element link to the next node

next

elem node

A B C D

Page 3: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 3

The Node Class for List Nodes

public class Node { // Instance variables: private Object element; private Node next; /** Creates a node with null references to its element and next node. */ public Node() { this(null, null); } /** Creates a node with the given element and next node. */ public Node(Object e, Node n) { element = e; next = n; } // Accessor methods: public Object getElement() { return element; } public Node getNext() { return next; } // Modifier methods: public void setElement(Object newElem) { element = newElem; } public void setNext(Node newNext) { next = newNext; }}

Page 4: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 4

Inserting at the Head1. Allocate a new

node2. update new

element3. Have new node

point to old head4. Update head to

point to new node

Page 5: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 5

Removing at the Head

1. Update head to point to next node in the list

2. Allow garbage collector to reclaim the former first node

Page 6: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 6

Inserting at the Tail1. Allocate a new node2. Insert new element3. Have new node

point to null4. Have old last node

point to new node5. Update tail to point

to new node

Page 7: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 7

Removing at the Tail

Removing at the tail of a singly linked list is not efficient!There is no constant-time way to update the tail to point to the previous node

Page 8: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 8

Stack with a Singly Linked List

We can implement a stack with a singly linked listThe top element is stored at the first node of the listThe space used is O(n) and each operation of the Stack ADT takes O(1) time

t

nodes

elements

Page 9: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 9

Queue with a Singly Linked List

We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node

The space used is O(n) and each operation of the Queue ADT takes O(1) time

f

r

nodes

elements

Page 10: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 10

List ADT (§ 5.2.3)

The List ADT models a sequence of positions storing arbitrary objectsIt establishes a before/after relation between positionsGeneric methods: size(), isEmpty()

Accessor methods: first(), last() prev(p), next(p)

Update methods: replace(p, e) insertBefore(p,

e), insertAfter(p, e),

insertFirst(e), insertLast(e)

remove(p)

Page 11: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 11

Doubly Linked ListA doubly linked list provides a natural implementation of the List ADTNodes implement Position and store:

element link to the previous node link to the next node

Special trailer and header nodes

prev next

elem

trailerheader nodes/positions

elements

node

Page 12: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 12

InsertionWe visualize operation insertAfter(p, X), which returns position q

A B X C

A B C

p

A B C

p

X

q

p q

Page 13: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 13

Insertion AlgorithmAlgorithm insertAfter(p,e):

Create a new node vv.setElement(e)v.setPrev(p) {link v to its predecessor}v.setNext(p.getNext()) {link v to its successor}(p.getNext()).setPrev(v){link p’s old successor to v}p.setNext(v) {link p to its new successor, v}return v {the position for the element e}

Page 14: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 14

DeletionWe visualize remove(p), where p = last()

A B C D

p

A B C

D

p

A B C

Page 15: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 15

Deletion Algorithm

Algorithm remove(p):t = p.element {a temporary variable to hold the return value}(p.getPrev()).setNext(p.getNext()) {linking out p}(p.getNext()).setPrev(p.getPrev())p.setPrev(null) {invalidating the position p}p.setNext(null)return t

Page 16: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 16

PerformanceIn the implementation of the List ADT by means of a doubly linked list The space used by a list with n

elements is O(n) The space used by each position of the

list is O(1) All the operations of the List ADT run in O(1) time

Operation element() of the Position ADT runs in O(1) time

Page 17: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 17

Terminologies

A Graph G=(V,E): V---set of vertices and E--set of edges.  

Path in G: sequence v1, v2, ..., vk of vertices in V such that (vi, vi+1) is in E.

vi and vj could be the same

Simple path in G: a sequence v1, v2, ..., vk of distinct vertices in V such that (vi, vi+1) is in E.

vi and vj can not be the same

Page 18: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 18

Example:

Simple path

A path, but not simple

Page 19: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 19

Terminologies (continued)

Circuit: A path v1, v2, ..., vk

such that v1 = vk

.

Simple circuit: a circuit v1, v2, ..., vk,where v1=vk and vivj for any 1<i, j<k.

Page 20: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 20

Euler circuit

Input: a graph G=(V, E) Problem: is there a circuit in G that

uses each edge exactly once.Note: G can have multiple edges, .i.e.,

two or more edges connect vertices u and v.

Page 21: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 21

Story: The problem is called Konigsberg bridge problem

it asks if it is possible to take a walk in the town shown in Figure 1 (a) crossing each bridge exactly once and returning home.

solved by Leonhard Euler [pronounced OIL-er] (1736)

The first problem solved by using graph theory A graph is constructed to describe the town. (See Figure 1 (b).)

Page 22: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 22

The original Konigsberg bridge (Figure 1)

Page 23: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 23

Theorem for Euler circuit (proof is not required)

Theorem 1 (Euler’s Theorem) The graph has an Euler circuit if and only if all the vertices of a connected graph have even degree.

Proof: (if) Going through the circuit, each

time a vertex is visited, the degree is increased by 2. Thus, the degree of each vertex is even.

Page 24: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 24

Proof of Theorem 1: (only if)

We give way to find an Euler circuit for a graph in which every vertex has an even degree.

Since each node v has even degree, when we first enter v, there is an unused edge that can be used to get out v.

The only exception is when v is a starting node. Then we get a circuit (may not contain all edges in

G) If every node in the circuit has no unused edge, all

the edges in G have been used since G is connected.

Otherwise, we can construct another circuit, merge the two circuits and get a larger circuit. In this way, every edge in G can be used.  

Page 25: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 25

An example for Theorem 1:

after merge

f

a

d

b

g

h

j

i

c

e

1

4

32

87

6 5

10

9

12

11

13

a b

c

1

3 2

d

feb

4

7 6

5

d

fea b

c

15 4

32

7 6

g

h

j

i

c

e

1213

8

910

11

Page 26: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 26

An efficient algorithm for Euler circuit

1. Starting with any vertex u in G, take an unused edge (u,v) (if there is any) incident to u2. Do Step 1 for v and continue the process until v has no

unused edge. (a circuit C is obtained)  3. If every node in C has no unused edge, stop. 4. Otherwise, select a vertex, say, u in C, with some unused edge incident to u and do Steps 1 and 2 until

another circuit is obtained. 5. Merge the two circuits obtained to form one circuit 6. Continue the above process until every edge in G is used.

 

Page 27: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 27

Euler Path

A path which contains all edges in a graph G is called an Euler path of G.

 Corollary: A graph G=(V,E) which

has an Euler path has 2 vertices of odd degree.

Page 28: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 28

Proof of the Corollary

Suppose that a graph which has an Euler path starting at u and ending at v, where uv.

Creating a new edge e joining u and v, we have an Euler circuit for the new graph G’=(V, E{e}).

  From Theorem 1, all the vertices in G’ have even degree. Remove e.   Then u and v are the only vertices of odd degree

in G. (Nice argument, not required for exam.) 

Page 29: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 29

Representations of Graphs

Two standard ways Adjacency-list representation

Space required O(|E|) Adjacency-matrix representation 

Space required O(n2).

Depending on problems, both representations are useful.

Page 30: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 30

Adjacency-list representation

Let G=(V, E) be a graph. V– set of nodes (vertices) E– set of edges.

For each uV, the adjacency list Adj[u] contains all nodes in V that are adjacent to u.

21

5 4

3

2

1

2

2

4

5 /

5

4 /

5

1

3 4 /

2 /

3 /

1

2

3

4

5

(a) (b)

Page 31: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 31

Adjacency-matrix representation

Assume that the nodes are numbered 1, 2, …, n. The adjacency-matrix consists of a |V||V| matrix

A=(aij) such that

aij= 1 if (i,j) E, otherwise aij= 0.

21

5 4

3

(a)

0 1 0 0 1 1 0 1 1 1

0 1 0 1 0

0 1 1 0 1

1 1 0 1 0

1 2 3 4 5

1

2

3

4

5

(c)

Page 32: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 32

Implementation of Euler circuit algorithm (Not required)

Data structures: Adjacency matrix Also, we have two lists to store the circuits

One for the circuit produced in Steps 1-2. One for the circuit produced in Step 4 We can merge the two lists in O(n) time.

In Step 1: when we take an unused edge (u, v), this edge is deleted from the adjacency matrix.

Page 33: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 33

Implementation of Euler circuit algorithm

In Step 2: if all cells in the column and row of v is 0, v has no unused edge.

1. Testing whether v has no unused edge.2. A circuit (may not contain all edges) is

obtained if the above condition is true.

In Step 3: if all the element’s in the matrix are 0, stop.

In step 4: if some elements in the matrix is not 0, continue.

Page 34: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 34

Summary of Euler circuit algorithm

Design a good algorithm needs two parts

1. Theorem, high level part2. Implementation: low level part.

Data structures are important. We will emphasize both parts.

Page 35: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 35

Summary Understand singly linked list

How to create a list insert at head, insert at tail, remove at head and

remove at tail. Should be able to write program using singly linked

list We will have chance to practice this.

Know the concept of doubly linked list. No time to write program about this.

Euler Circuit Understand the ideas No need for the implementation.

Page 36: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 36

My Questions: (not part of the lecture) Have you learn recursive call?

A function can call itself. .

Example: f(n)=n!=n×(n-1)×(n-2)×…×2×1 and 0!=1.It can also be written as f(n)=n×f(n-1) and

f(0)=1.Java code:

Public static int recursiveFactorial(int n) { if (n==0) return 1; else return n*recursiveFactorial(n-1);}

Page 37: Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence

Linked Lists 37

Remks

Delete Euler Circuit They do not like programming, especially, complicated programming work.