1 / 21

Homework 3 - Problem 3

Homework 3 - Problem 3. Use a linked list representation for storing adjacency information. 22C:021 Data Structures. myGraph – Present Organization. The adjacency information is retrieved from an array which stores edge information.

gaerwn
Download Presentation

Homework 3 - Problem 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Homework 3 - Problem 3 Use a linked list representation for storing adjacency information. 22C:021 Data Structures

  2. myGraph – Present Organization • The adjacency information is retrieved from an array which stores edge information. • If Vi and Vj has an edge, element [i,j] will be set to true. • To retrieve adjacency information for Viwe traverse row [i] in Edge array and return all [i,j] where [i,j] is true.

  3. myNewGraph – What needs to be done? • Instead of storing and retrieving adjacency information in/from an array, store/retrieve information in/from a linked list. • Edges variable in myGraph will now be an array of linked list instead of a two-dimensional array. • To lookup adjacency information for Vi we traverse linked list at Edges[i].

  4. What do we need? • A link class. • Stores information about a vertex (index?) • Stores information about the connection to the next element in the linked list (a reference?) • A Linked List class. • Stores the first (root) link • Provides methods to manipulate this list. • Good News: These classes are available!

  5. The Link Class - Changes • Presently stores an index and user-defined data. • Lets change it to • support strings • All our edge names are strings. • Support lookups using strings (instead of indexes) • Change two values associated with a link (index and data) to one (strings).

  6. The Link Class – Present Structure class Link { public int iData; // data item (key) public double dData; // data item public Link next; // next link in list // ------------------------------------------------------------ public Link(int id, double dd) // constructor { iData = id; dData = dd; } } Note: The displayLink method hasn’t been shown.

  7. The Link Class - New class Link { public String sData; // data public Link next; // next link in list // ------------------------------------------------------------- public Link(String data) // constructor { sData = data; } } Note: The displayLink Method would need to be modified accordingly.

  8. The LinkList class - Changes • Change the insertFirst method to allow passing in data as strings instead of (doubles or integers) • Change the find method to allow lookups using strings. • Change the delete method to allow deletes using a string.

  9. The LinkList Class - insertFirst Old: public void insertFirst(int id,double dd) { // make new link Link newLink = new Link(id, dd); newLink.next = first; // it points to old first link first = newLink; // now first points to this } New: public void insertFirst(String data) { // make new link Link newLink = new Link(data); newLink.next = first; // it points to old first link first = newLink; // now first points to this }

  10. The LinkList Class – find (Old) public Link find(int key) { Link current = first; // start at 'first' while(current.iData != key) // while no match, { if(current.next == null) // if end of list, return null; // didn't find it else current = current.next; // go to next link } return current; // found it }

  11. The LinkList Class – find (New) public Link find(String key) // find link with given key { // (assumes non-empty list) Link current = first; while(!current.sData.equals(key)) // while no match, { if(current.next == null) // if end of list, return null; else // not end of list, current = current.next; // go to next link } return current; // found it }

  12. The LinkList class – delete (Old) public Link delete(int key) // delete link with given key { // (assumes non-empty list) Link current = first; // search for link Link previous = first; while(current.iData != key) { if(current.next == null) return null; // didn't find it else { previous = current; // go to next link current = current.next; } } // found it if(current == first) // if first link, first = first.next; // change first else // otherwise, previous.next = current.next; // bypass it return current; }

  13. The LinkList class – delete (New) public Link delete(String key) // delete link with given key { // (assumes non-empty list) Link current = first; // search for link Link previous = first; while(!current.sData.equals (key)) { if(current.next == null) return null; // didn't find it else { previous = current; // go to next link current = current.next; } } // found it if(current == first) // if first link, first = first.next; // change first else // otherwise, previous.next = current.next; // bypass it return current; }

  14. Our new graph class- myNewGraph • What are we required to do? • Implement a new class similar to myGraph • Allow operations: • addVertex • addEdge • What else we might need? • A resize method to correctly resize arrays of type LinkedList • A method printAdjacencyList to print out adjacency information for a vertex.

  15. myNewGraph – Edges declaration protected LinkList[] Edges;

  16. myNewGraph – Constructor public myNewGraph(int capacity) { names = new String[capacity]; Edges = new LinkList[capacity]; for (int i = 0 ; i < capacity ; i ++) { Edges [i] = new LinkList(); } }

  17. myNewGraph – The new resize method protected LinkList[] resize(LinkList[] array, int newSize) { LinkList[] temp = new LinkList[newSize]; int smallerSize = newSize; if(array.length < smallerSize) smallerSize = array.length; for(int i = 0; i < smallerSize; i++) temp[i] = array[i]; for (int i = smallerSize ; i < temp.length ; i ++) temp [i] = new LinkList(); return temp; }

  18. myNewGraph – addVertex method • Direct reuse of our myGraph class • No changes required at all!

  19. myNewGraph – addEdge method • How will this be done? • For graphs: • Retrieve Adjacency lists for both the vertices • Add Vi to Adjacency list of Vj • Add Vj to Adjacency list of Vi • For digraphs: • Retrieve Adjacency lists for Vi • Add Vj to Adjacency list of Vi

  20. myNewGraph – addEdge method public void addEdge(String vertex1, String vertex2) { int i = getIndex(vertex1); if(i == -1) { System.out.print("addEdge failed: "); System.out.print(vertex1); System.out.println(" does not exist."); return; } int j = getIndex(vertex2); if(j == -1) { System.out.print("addEdge failed: "); System.out.print(vertex2); System.out.println(" does not exist."); return; } Edges [i].insertFirst (names [j]); Edges [j].insertFirst (names [i]); numEdges++; }

  21. Questions?

More Related