graphs 1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Graphs 1 PowerPoint Presentation
Download Presentation
Graphs 1

Loading in 2 Seconds...

play fullscreen
1 / 16

Graphs 1 - PowerPoint PPT Presentation

  • Uploaded on

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.
Download Presentation

PowerPoint Slideshow about 'Graphs 1' - beau

Download Now 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.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

Graphs 1

Kevin Kauffman

CS 309s

what are graphs
What are Graphs
  • Pie graphs? Bar graphs? WRONG
  • Graphs simply show things and their relationship to one another
  • 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
  • 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
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
graph representations adjacency matrix
Graph Representations Adjacency Matrix
  • 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
adjacency list
Adjacency List
  • 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
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
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
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
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
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
breadth first search bfs
Breadth First Search (BFS)


while(q isn’t empty)


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

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

q.add(other node)

what s going on
What’s going on?
  • We visit all the nodes in order of how far they are away
  • Since we directly reference the distance to the node as we pop it off the queue, we don’t have to worry about tracking exactly how far we are away
  • Instead of updating distance, we can do other things (perhaps add a “previous” value to get the “turn by turn” of what the path actually is)
depth first search dfs
Depth First Search (DFS)
  • Similar to BFS, but visit all nodes in a given sub-graph before entering another sub-graph (rather than visiting all nodes 1 away first)
  • Use a stack instead of a queue
  • GNYR 2009 I, Theta Puzzle