announcement
play

Announcement New exam dates: Graphs Exam 1 Monday, Oct 6 th - PDF document

Announcement New exam dates: Graphs Exam 1 Monday, Oct 6 th Exam 2 Monday, Nov 3 rd Terminology & Implementation Announcement Announcement The ACM is looking for a few good Actuallyso is Google programmers


  1. Announcement • New exam dates: Graphs – Exam 1 – Monday, Oct 6 th – Exam 2 – Monday, Nov 3 rd Terminology & Implementation Announcement Announcement • The ACM is looking for a few good • Actually…so is Google programmers • Google Code Jam – ACM Programming Contest – 1 st Round: Oct 1-Oct 15 • 3 Programmers per Team • Prelims – Sept 26 th • If interested, contact Paul Tymann (ptt@cs.rit.edu) – See by Wednesday. • http://topcoder.com/pl/?&module=Static&d1=googl e&d2=google_overview • Did I mention free food? Before we begin Graphs • Any questions? • A graph consists – Of a collection of points sometimes called vertices – Some pairs of points are connected by a line segment sometimes called an edge • Edges – May have a direction associated with them in which case the graph is called a directed graph ( digraph ) – A graph that contains edges that do not have a direction associated with them is called an undirected graph

  2. Graphs Graph edges A D • In addition to being directed or undirected, A D edges can be weighted or un-weighted. C C B B – A weighted edge has a value associated with it F F – The weight often measures the cost of using the E E edge to go from one node to another • A vertex may also have data associated with it. Graphs are usually drawn using points for vertices and lines for Edges, but a graph is defined independent of its representation Formal Definition Formal Definition – Edges • In an undirected graph • A graph G = ( V , E ), consists of – each edge e = < v 1 , v 2 > is an unordered pair of distinct – V, a set of vertices vertices – E, a set of edges where each edge is a pair of • In a directed graph distinct elements from V – each edge e = < v 1 , v 2 > is an ordered pair of distinct vertices – < v 1 , v 2 > ≠ < v 2 , v 1 > • For weighted edges, weight can have a scalar value, w, associated with it – < v 1 , v 2 , w > Formal definition example Formal definition example • V = { A, B, C, D, E, • V = { A, B, C, D, E, F} F} 10 • E = { <A, B>, • E = { <A, B, 5>, A D A D 5 • <B,C>, 15 • <B,C, 20>, 25 C C B B • <A,C>, • <A,C, 15>, 20 F F • <A,D>, • <A,D, 10>, 30 E E • <C,D>, • <C,D, 25>, • <E,F> } • <E,F, 30> }

  3. Terminology Terminology – Paths and Cycles • The degree of a vertex x is the number of • Two different vertices, x and y , in a graph edges e in which x is one of the endpoints of are said to be adjacent if an edge connects x edge e to y • The neighbors of a vertex v , are the vertices – I.e. if <x,y> is an edge, x and y are adjacent that are directly connected to v Terminology – Paths and Cycles Paths and Cycles -- Example A D • A path is a sequence of vertices in which each • ADCFDC is a path vertex is adjacent to the next one • ADC is a simple path C B – V 1 , V 2 , …, V n and <V i , V i+1 > is an edge. • A simple path is a path in which no vertex is F • ADCA is a cycle repeated E • A cycle is a path of length greater than one that • ADFECBA is a begins and ends at the same vertex Hamiltonian Cycle. • A Hamiltonian cycle is a cycle that visits all vertices exactly once. Connectedness Connectedness – Example A D • A graph is connected if A D – For every pair of nodes A, B, there is a simple C C B B path from A to B. F F E E • A subset of nodes that are connected define a connected subgraph Not connected Connected {A,B,C,D} and {E,F} form connected subgraphs

  4. Terminology Building a Graph • Vertex, Edges (weighted and unweighted) import java.util.*; • Directed vs. undirected public interface DiGraph { • Paths and Cycles // Methods to build the graph public void addVertex( Object key, Object data ); public void addEdge( Object fromKey, Object toKey, • Connectedness Object data ) throws NoSuchVertexException; ... } // DiGraph • Questions so far? Building a Graph Graph Implementations • Adjacency Matrix addVertex (“A”, “A”); 2 • Adjacency List A B addVertex (“B”, “B”); addVertex (“C”, “C”); addEdge (“B”, “C”, new Integer(5)); 6 7 5 addEdge (“A”, “B”, new Integer(2)); addVertex (“D”, “D”); addEdge (“A”, “D”, new Integer(6)); D C addEdge (“B”, “D”, new Integer(7)); Adjacency Matrix Adjacency Matrix • Assumes vertices are numbered from 0 – n- • For unweighted edges, array elements are 1. boolean. 3 • Graph represented by a N x N, 2 0 1 2 3 4 dimensional array. 0 F F T F T 2 4 1 F F F F F • Array element (i, j) indicate in there is an 2 T F F T T edge between vertex i and vertex j 3 F F T F T 4 T F T T F 1 0 T = edge F = no edge

  5. Adjacency Matrix Adjacency Matrix • For weighted edges, array elements can • Array elements might also contain objects hold weights. of an Edge class. 3 3 6.1 6.1 0 1 2 3 4 0 1 2 3 4 1.5 1.5 NULL 1.0 NULL 3.7 0 ∞ ∞ 1.0 ∞ 3.7 NULL 0 4.2 4.2 NULL NULL 2 4 2 4 NULL NULL NULL 1 ∞ ∞ ∞ ∞ ∞ 1 2 1.0 2 1.0 ∞ ∞ 6.1 4.2 1.0 3.7 1.0 3.7 NULL NULL 6.1 4.2 NULL 1.5 3 ∞ ∞ 6.1 ∞ 1.5 NULL NULL 6.1 3 NULL 4.2 1.5 4 3.7 1 0 1 0 NULL 4 3.7 ∞ 4.2 1.5 ∞ ∞ indicates no edge NULL indicates no edge Adjacency Matrix Adjacency List • Things to note: • Assumes vertices are numbered from 0 – n- 1. – For an undirected graph, only half of the matrix is needed. • Graph is represented by an array of lists, – Lots of wasted space if not a lot of edges in the one list for each vertex. graph. • Elements of list corresponding to a vertex indicates edges to neighboring vertices. Adjacency List Adjacency List • For unweighted edges, list nodes simply • For weighted edges, list nodes give the give the neighboring vertex neighboring vertex and the weight 3 3 6.1 0 2 4 0 2 4 1.5 1.0 3.7 2 4 1 1 4.2 2 4 3 0 4 2 3 0 4 2 6.1 1.0 4.2 3.7 3 3 1.0 2 4 2 4 6.1 1.5 4 4 3 2 0 3 2 0 1 0 1.5 4.2 3.7 1 0

  6. Adjacency List Graph Implementations • List elements might also contain objects of • Graph Vertices an Edge class. – Very often, other data may be associated with a vertex 3 6.1 2 4 0 – It is often useful to create a Vertex class that 1.5 1.0 3.7 holds all data. 1 4.2 2 4 2 3 0 4 – Each graph may, in addition to edge 6.1 1.0 4.2 3.7 representation, contain a set of Vertex class 1.0 3 2 4 6.1 1.5 objects. 4 3 2 0 1.5 4.2 3.7 1 0 Graph Implementations Graph Implementations • Comparison – space • Comparison Time – Graph of 50 Vertices – 4bytes / entry Operation Adj. Matrix Adj List Edges Adj. Matrix Adj List Insert Node O (1) O (E / N) 49 10,000 792 Insert Edge O (1) O (E / N) 100 10,000 1,200 Delete Node O (N) O (E) 250 10,000 2,400 Delete Edge O (1) O (E / N) 500 10,000 4,400 Find Neighbors O(N) O (E / N) 1,000 10,000 8,400 N = number of nodes 1,200 10,000 10,000 1,500 10,000 12,400 E = number of edges 2,000 10,000 16,400 E / N = avg number of edges per vertex 2,500 10,000 20,400 Graph Implementations Summary • When implementing CS3DiGraph • Graphs • Terminology – Choose implementation best suited for TSP • Building a Graph – Feel free to use nested classes for aux classes (if required) • Implementing a Graph – Feel free to use Java Collections Framework • Tomorrow: Graph Traversals • Questions?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend