Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Speeding up Dijkstra's Algorithm

Demonstrates a way of speeding up the O(n<sup>2</sup>) version of Dijkstra's Algorithm by about 4x.

Here, dist[][] is the adjacency matrix representing the graph and n is the number of nodes. d2[] is where the lengths of the shortest paths are saved.

For a full explanation, see this.

void dijkstra2(int s) {
	int queue[GRAPHSIZE];
	char inQueue[GRAPHSIZE];
	int begq = 0,
	    endq = 0;
	int i, mini;
	int visited[GRAPHSIZE];

	for (i = 1; i <= n; ++i) {
		d2[i] = INFINITY;
		visited[i] = 0; /* the i-th element has not yet been visited */
		inQueue[i] = 0;
	}

	d2[s] = 0;
	queue[endq] = s;
	endq = (endq + 1) % GRAPHSIZE;


	while (begq != endq) {
		mini = queue[begq];
		begq = (begq + 1) % GRAPHSIZE;
		inQueue[mini] = 0;

		visited[mini] = 1;

		for (i = 1; i <= n; ++i)
			if (dist[mini][i])
				if (d2[mini] + dist[mini][i] < d2[i]) { 
					d2[i] = d2[mini] + dist[mini][i];
					if (!inQueue[i]) {
						queue[endq] = i;
						endq = (endq + 1) % GRAPHSIZE;
						inQueue[i] = 1;
					}
				}
	}
}

My handy mp3 "alarm clock" written in Ruby.

// Simple tool to help wake me up in the morning. Not the cleanest code, but it took no time to write. Ruby rocks!

#!/bin/env ruby
#
# A helper to setup an alarm to slowly and leisurely wake up in the morning.
#
# $Id: mp3wakeup.rb 6 2007-01-31 16:02:57Z btek $
#

if ARGV.length < 1
	puts "Usage: #{$0} <time> [mp3dir]"
	exit 1
end

#############################
# Configuration parameters. #
#############################

NUM_STEPS = 15
STEP_DURATION = 60 * 1

FINAL_VOLUME = 95
INITIAL_VOLUME = 30
VOLUME_STEP = (FINAL_VOLUME - INITIAL_VOLUME) / NUM_STEPS

MIN_SEC_BEFORE_ALARM = NUM_STEPS * STEP_DURATION

#############################
#############################

time = Regexp.quote ARGV[0]
mp3dir = Regexp.quote(ARGV[1]) if ARGV.length > 1

def get_alarm_time(desired_hour, desired_minute)
  now = Time.new
  
  alarm_time = Time.local(now.year, now.month, now.day, desired_hour, desired_minute, 0, 0)
  
  if alarm_time - now < MIN_SEC_BEFORE_ALARM
    alarm_time = alarm_time + 60*60*24
  end
  
  return alarm_time
end

def run(cmd)
  #puts cmd
  system cmd
end

puts "Waking you up by #{time} with songs from #{mp3dir}."

match = time.match(/(\d+)(:(\d+))?/)
hour = match[1].to_i
minute = if (match.length > 1)
           match[3].to_i 
         else
           0
         end

alarm_time = get_alarm_time(hour, minute)

for i in 0..NUM_STEPS
  alert_time = alarm_time - (STEP_DURATION * i)
  volume = FINAL_VOLUME - (VOLUME_STEP * i)
  
  at_cmd = "at #{alert_time.hour.to_s.rjust(2, '0')}:#{alert_time.min.to_s.rjust(2, '0')}"
  run "echo amixer sset PCM #{volume}% | #{at_cmd}"
  
  if i == NUM_STEPS
    # Start playing after time is confirmed set.
    alert_time = alert_time + (60)
    at_cmd = "at #{alert_time.hour.to_s.rjust(2, '0')}:#{alert_time.min.to_s.rjust(2, '0')}"
    
    if mp3dir != nil
      run "echo audacious #{mp3dir} | #{at_cmd}"
    else
      run "echo audtool playback-play | #{at_cmd}"
    end
  end
end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS