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.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Chapter 3 Lists Dr Zeinab Eid.
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
6/7/2014 8:24 AMSequences1 Lists and Sequences. 6/7/2014 8:24 AMSequences2 Outline and Reading Singly linked list Position ADT and List ADT (§5.2.1) Doubly.
Chapter 8 Topics in Graph Theory
Walks, Paths and Circuits Walks, Paths and Circuits Sanjay Jain, Lecturer, School of Computing.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
1 Lecture 5 (part 2) Graphs II Euler and Hamiltonian Path / Circuit Reading: Epp Chp 11.2, 11.3.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Review Learn about linked lists
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
Section 14.1 Intro to Graph Theory. Beginnings of Graph Theory Euler’s Konigsberg Bridge Problem (18 th c.)  Can one walk through town and cross all.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
Two-Dimensional Arrays Introduction to Linked Lists COMP53 Sept
Discrete Structures Chapter 7B Graphs Nurul Amelina Nasharuddin Multimedia Department.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
© 2004 Goodrich, Tamassia Stacks1 Abstract Data Types (ADTs) An abstract data type (ADT) is a contract between the user of a data structure and its implementor.
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Representing Graphs Wade Trappe. Lecture Overview Introduction Some Terminology –Paths Adjacency Matrix.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
GRAPH Learning Outcomes Students should be able to:
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Chapter 2 Graph Algorithms.
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Unit – V Graph theory. Representation of Graphs Graph G (V, E,  ) V Set of vertices ESet of edges  Function that assigns vertices {v, w} to each edge.
Lecture 10: Graph-Path-Circuit
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
COSC 2007 Data Structures II
Eulerian Paths and Cycles. What is a Eulerian Path Given an graph. Find a path which uses every edge exactly once. This path is called an Eulerian Path.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
1 GRAPH Learning Outcomes Students should be able to: Explain basic terminology of a graph Identify Euler and Hamiltonian cycle Represent graphs using.
1 Lecture 5 (part 2) Graphs II (a) Circuits; (b) Representation Reading: Epp Chp 11.2, 11.3
LINKED LISTS.
Linked List, Stacks Queues
Data Structure Dr. Mohamed Khafagy.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
Euler Paths and Circuits
Data Structures Linked list.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
LINKED LISTS CSCD Linked Lists.
Graphs Chapter 13.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
Representing Graphs Wade Trappe.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
CS3335: Design and Analysis of Algorithms
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Presentation transcript:

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 of nodes Each node stores element link to the next node next elem node ABCD 

Linked Lists3 The Node Class for List Nodes (the file is source/Node.java) public classNode{ // 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; }

Linked Lists4 Inserting at the Head 1. Allocate a new node 2. update new element 3. Have new node point to old head 4. Update head to point to new node

Linked Lists5 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

Linked Lists6 Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node

Linked Lists7 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 The interface of data structure list is in List.java. The implementation is in NodeList.java. But it uses DNode.java. Actually, it is doubly linked list.

Linked Lists8 Stack with a Singly Linked List We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time  t nodes elements

Linked Lists9 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

Linked Lists10 List ADT (§ 5.2.3) The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic 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)

Linked Lists11 Doubly Linked List A doubly linked list provides a natural implementation of the List ADT Nodes implement Position and store: element link to the previous node link to the next node Special trailer and header nodes prevnext elem trailer header nodes/positions elements node

Linked Lists12 Insertion We visualize operation insertAfter(p, X), which returns position q ABXC ABC p ABC p X q pq

Linked Lists13 Insertion Algorithm Algorithm insertAfter(p,e): Create a new node v v.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}

Linked Lists14 Deletion We visualize remove(p), where p = last() ABCD p ABC D p ABC

Linked Lists15 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

Linked Lists16 Performance In 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

Linked Lists17 Terminologies A Graph G=(V,E): V---set of vertices and E--set of edges. Path in G: sequence v 1, v 2,..., v k of vertices in V such that (v i, v i+1 ) is in E. v i and v j could be the same Simple path in G: a sequence v 1, v 2,..., v k of distinct vertices in V such that (v i, v i+1 ) is in E. v i and v j can not be the same

Linked Lists18 Example: Simple path A path, but not simple

Linked Lists19 Terminologies (continued) Circuit: A path v 1, v 2,..., v k such that v 1 = v k. Simple circuit: a circuit v 1, v 2,..., v k, where v 1 =v k and v i  v j for any 1<i, j<k.

Linked Lists20 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.

Linked Lists21 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).)

Linked Lists22 The original Konigsberg bridge (Figure 1)

Linked Lists23 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.

Linked Lists24 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.

Linked Lists25 An example for Theorem 1: after merge f a d b g h j i c e ab c 1 32 d f e b d f e ab c g h j i c e

Linked Lists26 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 u 2. 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.

Linked Lists27 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.

Linked Lists28 Proof of the Corollary  Suppose that a graph which has an Euler path starting at u and ending at v, where u  v.  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.)

Linked Lists29 Representations of Graphs  Two standard ways  Adjacency-list representation  Space required O(|E|)  Adjacency-matrix representation  Space required O(n 2 ).  Depending on problems, both representations are useful.

Linked Lists30 Adjacency-list representation  Let G=(V, E) be a graph.  V – set of nodes (vertices)  E – set of edges.  For each u  V, the adjacency list Adj[u] contains all nodes in V that are adjacent to u / 5 4/ / 2/ 3/ (a)(b)

Linked Lists31 Adjacency-matrix representation  Assume that the nodes are numbered 1, 2, …, n.  The adjacency-matrix consists of a |V|  |V| matrix A=(a ij ) such that a ij = 1 if (i,j)  E, otherwise a ij = (a) (c)

Linked Lists32 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.

Linked Lists33 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.

Linked Lists34 Summary of Euler circuit algorithm  Design a good algorithm needs two parts 1. Theorem, high level part 2. Implementation: low level part. Data structures are important.  We will emphasize both parts.

Linked Lists35 Summary (Subject to change)  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.

Linked Lists36 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); }

Linked Lists37 Remks  Delete Euler Circuit  They do not like programming, especially, complicated programming work.