220 likes | 360 Views
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.
E N D
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. • 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.
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].
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!
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).
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.
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.
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.
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 }
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 }
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 }
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; }
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; }
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.
myNewGraph – Edges declaration protected LinkList[] Edges;
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(); } }
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; }
myNewGraph – addVertex method • Direct reuse of our myGraph class • No changes required at all!
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
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++; }