Download presentation
Presentation is loading. Please wait.
1
COMP 261 Lecture 2 Graphs 1 of 3
2
Outline Graph Learning objectives Assignments 1 and 2
Understand the data files Read the files Data structures for graphs
3
Graph Collection of nodes
Collection of edges (connections between nodes) Many situations in the world places/objects with connections airports & flights, intersections & roads, network switches and cables …. entities with relationships social networks, biological models web pages …. states and actions games, plans, …..
4
Graph Learning objectives Big block!
Produce a Google Maps-like program Load & Display the map Move / Zoom in and out Store relevant information Show matching list by name prefix Highlight roads / places Shortest path finding … Big block! ~ 8 lectures 2 assignments
5
Graph Learning objectives
Design proper data structure to store graph (nodes, edges, …) Display graph (zoom in/out, move, …) Search the graph pop-up matching street names while typing shortest path finding Understand the graph Connected or disconnected? Points that are critical for connectivity
6
Auckland Roads: Assignment 1
Load data into data structures graph of intersections and roads indexes for fast searching: trie of road names Display the map (and zoom in/out) Select roads (by name) Show all roads matching what is typed so far Highlight road on map. Select intersections (by mouse click) Highlight intersection Display names of roads at intersection. Run the example program.
7
Auckland Roads: Assignment 2
Google maps / GPS navigation system: Find shortest routes in graph between two intersections Emergency planning Identify disconnected parts of the road system Identify all intersections that would disconnect part of the system. All build on the graph structure of Assignment 1.
8
Assignment 1: step by step
Eclipse: “hello world” program Technical notes: online eclipse tutorial GUI.java is provided for you as abstract class Example of its use is in SquaresExample.java Your job is about representing the graph and drawing it similar to how squares are drawn! Data files: load data each file can be a different object draw: x, y build/store the graph
9
SquaresExample Interface
Load data Zoom in/out (+/-) Move Search street name Display Text field
10
Road Map program Main program
Needs field(s) to hold the road map data structures Main method / constructor filling the field(s) Learn to fulfil the GUI methods (redraw, onClick, onSearch, onMove, onLoad …) to make interesting things happen Classes for RoadGraph, Roads, Intersections, Location… Each class has the data structure and methods for loading/accessing/modifying
11
Program Summary Load the files Store the information into a graph
Roads: roadID-roadInfo.tab Nodes: nodeID-lat-lon.tab Road segments: roadSeg-roadID-length-nodeID-nodeID-coords.tab Store the information into a graph Data structure needed Display (part of) the graph in the drawing area Redraw the graph after movements (e.g. left, right, zoom in/out)
12
Loading Files Roads (roadID-roadInfo.tab) roadid type name city 1way sp rc !car !ped !bic cowley st waterview walmer rd point chevalier carrington rd point chevalier Nodes (nodeID-lat-lon.tab) Road segments (roadSeg-roadID-length-nodeID-nodeID-coords.tab) roadID length nodeID1 nodeID2 coords Read README.txt
13
Loading Files roads vs road segments
A road can have many intersections (e.g. Williamson Ave) A road segment has no intersection in the middle node = intersection segments nodes road
14
Data Properties Road ID Type (no need to use) Name (match the search)
City One-way or not (needed for shortest path finding) Speed limit (needed for fastest path finding) Road class (no need given speed limit) notforcar, notforpede, notforbicy (special use, not needed for now)
15
Data Properties Node (No column title in file!) ID Latitude Longitude
Segment RoadID (which road it belongs to) Length (needed for shortest path finding) NodeID1 (first node) NodeID2 (second node) Coords (for display, the segment might not be straight) Direction of segment? NodeID2 Coord4 Coord2 Coord3 Coord1 NodeID1
16
Reading data more efficiently
File FileReader BufferedReader // Read file line by line File roadFile = new File(dataDirectory+"roadID-roadInfo.tab"); BufferedReader data = new BufferedReader(new FileReader(roadFile)); String line = data.readLine(); // Process each line using split method String[] values = line.split("\t"); int n = Integer.parseInt(values[0]); double d = Double.parseDouble(values[1]); Columns split by \t (tab) Convert String to proper type!
17
Data Structures for Graphs
Straightforward design according to the columns in the file (each column is a field/variable in the class) intersections = nodes, road segments = edges Nodes Edges A B C D E F G H 15 A C 3 A F 12 B D 18 D B 6 B H 71 C B ……
18
Data Structures for Graphs
A graph is typically represented as a set of nodes and a set of edges Graph Set<Node> nodes Set<Edge> edges Node Information of a node: id, location, … Edge Information of an edge: id, two end-nodes, direction, …
19
Data Structures for Graphs
Different types of graphs Multi-graph (multi-lane roads) Directed edges (one-way streets) Loops Other complex properties No turn right/left … Represent an undirected edge as two directed edges? 22 C A 22 A C
20
More Data Structures for Graphs
What do we need to do with a graph? Common actions on a graph List all the nodes List all the edges Find all outgoing edges of a node Find all incoming edges of a node Find all the outgoing neighbours of a node Find all the incoming neighbours of a node Find out whether two nodes are connected or not Find the edge between two nodes … What do we need for the Road Map? maybe same if undirected
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.