Graphs 1

1 / 16

Graphs 1 - PowerPoint PPT Presentation

Graphs 1. Kevin Kauffman CS 309s. What are Graphs. Pie graphs? Bar graphs? WRONG Graphs simply show things and their relationship to one another. Examples. Actors and who they were in movies with (oracle of bacon) Family members and who is related to whom (family tree)

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

PowerPoint Slideshow about ' Graphs 1' - beau

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Graphs 1

Kevin Kauffman

CS 309s

What are Graphs
• Pie graphs? Bar graphs? WRONG
• Graphs simply show things and their relationship to one another
Examples
• Actors and who they were in movies with (oracle of bacon)
• Family members and who is related to whom (family tree)
• Intersections and the roads that connect them
• Board game spaces and which moves you can make
Terminology
• Node (vertex): the individual thing we are keepting track of (intersection, family member, actor, game space)
• Edge: the relationship between TWO nodes (road, relationship, game move, shared movie)
• A given set of nodes and edges is called a graph
Graph Dichotomies
• Directed/Undirected
• Edges may be one way (“go directly to jail” in monopoly) or both ways (two actors in the same movie)
• Weighted/Unweighted
• Edges may have a value which represents some quality (length of a road between two intersections) or every edge can be valued equally (actors in the same movie)
• Cyclic/Acyclic
• Can you start at one node, and follow edges (any one edge at most once) and get back to the start node (a cycle)?
• Undirected, Acyclic graphs are known as TREES
• 2-D array indicating whether there is an edge between two given nodes
• int[][]
• In unweighted graphs, int[i][j]=1 indicates edge, while 0 indicates no edge
• In weighted graphs, int[i][j] indicates length
• 0,-1, or some huge number may indicate no edge
• O(n^2) storage, O(n^2) iterate through all edges….WASTEFUL
• Only store edges which actually exist (instead of all possible edge)
• For each node, store each node to which it is connected
• Unweighted: ArrayList<ArrayList<Integer>>
• Weighted: Arraylist<HashMap<Integer, Integer>>
• O(E) to iterate through all edges (better) but pain in the neck to initialize and use
What can we do with graphs?
• Loads of stuff! (and we’ll get to lots of it throughout the semester)
• Calculate distance between nodes
• Figure out which nodes can be reached from others
• Figure out which edges we can remove and still get to all the other nodes
• ?????
Distance in an Unweighted Graph
• If we have an unweighted graph (can be either directed or not), how can we figure out the minimum number of edges we must traverse to get from some node s to another node t?
• Ideas?
Brute Force Every Path
• Start at s and follow every edge there
• At each node you reach, follow every edge there, recording the distance to each node
• At the end, take the minimum distance to the node
• Problem? Takes O(n!) time to compute!
Key Intuition
• We need to come up with a way to only visit a given node once
• If we can visit all the nodes 1 away from s first, then we don’t need to visit them again (because we know we can’t beat that!)
• If we can visit all the nodes 2 away from s next, we don’t need to visit THEM again (since we already know they can’t be 1 away)
Distance (overview)
• Can we find all nodes 1 away from s?
• Yes…all nodes which have an edge going to s must be at most 1 away from s
• Can we find all nodes 2 away from s?
• Yes… all the nodes (which we haven’t visited) which have an edge from one of the step 1 nodes must be 2 away from s
• By induction, we can find the distance to all the nodes

while(q isn’t empty)

on=q.poll()

for(edge in on) if (other node unvisited)

distace(other node)=distance(on)+1