1 / 6

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms. Lecture 20: Graphs IV. 0. 1. 0. 0. 1. 1. 0. 1. 0. 1. 0. 0. 0. 1. 1 2 3 4 5 6 7. 2. 5. 6. 0. 1. 0. 1. 0. 0. 0. 3. 1. 7. 0. 0. 1. 0. 1. 0. 1. PVD. 849. 2. 4. ORD. 1843. 1. 0. 0. 1. 0. 1. 1. 142. SFO. 3.

learl
Télécharger la présentation

CSE 373: Data Structures and Algorithms

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. CSE 373: Data Structures and Algorithms Lecture 20: Graphs IV

  2. 0 1 0 0 1 1 0 1 0 1 0 0 0 1 1 2 3 4 5 6 7 2 5 6 0 1 0 1 0 0 0 3 1 7 0 0 1 0 1 0 1 PVD 849 2 4 ORD 1843 1 0 0 1 0 1 1 142 SFO 3 7 5 1 0 0 0 1 0 0 802 6 1 7 4 LGA 0 1 0 1 1 0 0 1743 337 1 5 1387 HNL 2555 4 5 2 1099 1233 LAX DFW 1120 MIA Practical implementation • Not all graphs have vertices/edges that are easily "numbered" • how do we actually represent 'lists' or 'matrices' of vertex/edge relationships? How do we quickly look up the edges and/or vertices adjacent to a given vertex? • Adjacency list: Map<V, List<V>> • Adjacency matrix: Map<V, Map<V, E>>

  3. Maps and sets within graphs since not all vertices can be numbered, we can use: 1. adjacency list • each Vertex maps to a List of edges or adjacent Vertices • Vertex --> List of Edges • to get all edges adjacent to V1, look upList<Edge> neighbors = map.get(V1) 2. adjacency adjacency matrix map • each Vertex maps to a Hash of adjacent • Vertex --> (Vertex --> Edge) • to find out whether there's an edge from V1 to V2, call map.get(V1).containsKey(V2) • to get the edge from V1 to V2, call map.get(V1).get(V2)

  4. Implementing Graph with Adjacency List public interface Graph<V> { public void addVertex(V v); public void addEdge(V v1, V v2, int weight); public boolean hasEdge(V v1, V v2); public Edge<V> getEdge(V v1, V v2); public boolean hasPath(V v1, V v2); public List<V> getDFSPath(V v1, V v2); public String toString(); }

  5. Edgeclass public class Edge<V> { public V from, to; public int weight; public Edge(V from, V to, int weight) { if (from == null || to == null) { throw new IllegalArgumentException("null"); } this.from = from; this.to = to; this.weight = weight; } public String toString() { return "<" + from + ", " + to + ", " + weight + ">"; } }

  6. VertexInfo class public class VertexInfo<V> { public V v; public boolean visited; public VertexInfo(V v) { this.v = v; this.clear(); } public void clear() { this.visited = false; } }

More Related